Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orientation change is reseting the days in the calendar #557

Closed
Alvin12344 opened this issue Sep 9, 2017 · 29 comments
Closed

Orientation change is reseting the days in the calendar #557

Alvin12344 opened this issue Sep 9, 2017 · 29 comments

Comments

@Alvin12344
Copy link

It looks like as soon as orientation changes, the calendar resets to the month of end date but does not change the month and year label. That is if I am in June 2017 in landscape and change it to portrait, the days reset to September 2017 but month and label stays as June 2017. How do I stop this from happening?

I am using version 7.0.6 with Swift 3.0

@patchthecode
Copy link
Owner

I have a function to help you in that area, but its has a small bug which was fixed by the master branch.
Can you update to master branch code?

put this in pod file

pod 'JTAppleCalendar', :git => 'https://github.com/patchthecode/JTAppleCalendar.git', :branch => 'master'

Then do pod update.

You will be forced to add a new function called willDisplayCell. This is explained here under Problem#1.

Once your update is complete, lets deal with the rotation problem.
then implement this function in your viewController

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    let theDate = // the date you want the calendar to focus to on orientation change
    calendarView.viewWillTransition(to: size, with: coordinator, anchorDate: theDate)
}

let me know how it goes

@Alvin12344
Copy link
Author

Thanks for your reply, how do I update master branch code? Can you please explain.

@patchthecode
Copy link
Owner

patchthecode commented Sep 10, 2017

I just did above.

put this in pod file

pod 'JTAppleCalendar', :git => 'https://github.com/patchthecode/JTAppleCalendar.git', :branch => 'master'

Then do pod update.

@Alvin12344
Copy link
Author

Alvin12344 commented Sep 10, 2017

I did it but what should I add to the function

func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
<#code#>
}

@patchthecode
Copy link
Owner

can you join me here -> https://gitter.im/patchthecode/JTAppleCalendar

@josjevv
Copy link

josjevv commented Jan 15, 2018

I am experiencing this issue with version 7.1.5 (using Carthage). I added the viewWillTransition method (as well as willDisplayCell). The current visibleDates has been lost after rotation and the calendar shows the first date as configured in configureCalendar method.

Is it possible this bug has been reintroduced?

@patchthecode
Copy link
Owner

patchthecode commented Jan 15, 2018

@josjevv

  1. Can you show me the code you did?
  2. Can you tell me the device you run it on? if it is simulator please let me know the device type/name

@josjevv
Copy link

josjevv commented Jan 16, 2018

It's a private repo so I can't easily share the full code base. It's running on iPhone 6s simulator (11.2) but also happens on a real device (also iPhone 6s on 11.2). Relevant code:

set selected date

    func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
        self.handleCellConfiguration(cell: cell, cellState: cellState, date: date)
        
        self.selectedDate = date
    }

helper function (anchorDate is current selected date)

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if self.calendarView != nil {
            let theDate = self.selectedDate
            NSLog("anchorDate: \(theDate)")
            self.calendarView.viewWillTransition(to: size, with: coordinator, anchorDate: theDate)
        }
    }

configure

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        let current = Date()
        guard
            let startDate = self.calendar.date(byAdding: .year, value: -1, to: current),
            let endDate = self.calendar.date(byAdding: .year, value: 2, to: current) else {
                fatalError("Dates could not be calculated")
        }
        
        let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)

        return parameters
    }

So after rotating the calendar shows januari 2017 (as configured using startDate)

@josjevv
Copy link

josjevv commented Jan 22, 2018

@patchthecode do you have enough information to debug this issue? Thanks in advance.

@patchthecode
Copy link
Owner

patchthecode commented Jan 22, 2018

sorry. i missed this.
let me re-open it.

Wil debug this today.

@patchthecode
Copy link
Owner

I am not experiencing the issue that you are facing.
is it possible you can create a new sample project?
The only code in it can just be that rotation code.

I have tried it in may way on my example project attached to this github project. And I cant reproduce it at all.

@josjevv
Copy link

josjevv commented Jan 30, 2018

Hi,

initially I could not reproduce it either. I kept adding functionality in the example project from the real app. It turns out it starts happening when I added Variations in storyboard based on height. See this example project I have setup for you: https://github.com/SocialSchools/JTAppleCalendarRotationTest

In the (main) storyboard, there is a Main Stack View, which is Vertical by default and Horizontal for compact height only. If I remove that variation, it works fine again. In the real app the events for the selected date is shown. I simplified that by only showing the selected date.

@patchthecode
Copy link
Owner

ok. will take a look.

@JettF
Copy link

JettF commented Feb 6, 2018

I am seeing this issue too. I'll see if I can drum up an example project that replicates it.

@josjevv
Copy link

josjevv commented Feb 6, 2018

is the sample project I provided not applicable for you? https://github.com/SocialSchools/JTAppleCalendarRotationTest

@patchthecode
Copy link
Owner

It is. I just got super busy this week.
Will take a look soon ok.

@josjevv
Copy link

josjevv commented Feb 6, 2018

hi, sorry the comment was meant for @JettF. Take it easy, I am already glad you can look into it.

@JettF
Copy link

JettF commented Feb 6, 2018

@josjevv after looking at it, your project is applicable to me. My mistake.

@vldalx
Copy link

vldalx commented Mar 28, 2018

got the same error in case of device rotation.

@NWITS
Copy link

NWITS commented Apr 4, 2018

me too...

@patchthecode
Copy link
Owner

patchthecode commented Apr 4, 2018

checking this out today. I was tied up earlier.

@patchthecode
Copy link
Owner

@josjevv sorry i took so long. I got really tied up on other projects.
I see your issue. I'm thinking of a fix. I didnt consider the fact about different size classes.

@jimmyhiguchi
Copy link

I am also experiencing this issue. Let me know if anyone found a work around. Thanks!

@msencenb
Copy link

I ran into this issue too and have a workaround for my situation that others are welcome to use.

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        let selectedDates = JTCalendarView.selectedDates
        if selectedDates.count > 0 {
            JTCalendarView.scrollToDate(selectedDates.first!, animateScroll: false)
            JTCalendarView.selectDates(selectedDates)
        }
    }

This works in my case since I select a date (today) when the view is first loaded up. That means selectedDates is guaranteed to have at least one member and the calendar can simply scroll back to it and re-select that date during rotation or size changes.

I am sure there is a more elegant solution that can be made in the internals of JTAppleCalendar, but works ok for my use case.

P.S. Thank you for such an awesome library @patchthecode , I managed to get a full calendar UI up in just a few days (I was dreading making a custom collectionview)!

@NickGaens
Copy link

@patchthecode Any chance this library will be getting a fix for this issue as it's quite annoying? The workaround of @msencenb above works, but I'd rather not include such a workaround in the first place.

A major thanks as well from me for this awesome library, by the way; it's really great.

@patchthecode
Copy link
Owner

patchthecode commented May 25, 2018

@NickGaens
will work on this.
i'm just a little caught up at the moment.
but it is on my priority list.
Will take look over the weekend.

@jaybayley
Copy link

An alternative to @msencenb's solution is:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    calendarView.viewWillTransition(to: size, with: coordinator, anchorDate: self.visibleMonthStartDate)
}

This worked better in my case as I use the didSelectDate delegate method to perform a segue and so don't have any need for selectedDates.

@patchthecode
Copy link
Owner

@jaybayley
this is how it is supposed to be done.
but the guys here seem to imply that it is not working.

A sample app was given, above with demonstrated the bug. I have not found a fix yet (sort of caught up between more than one projects)

@patchthecode
Copy link
Owner

patchthecode commented Aug 27, 2018

I made an update to master branch.
Can you guys check and see if it was fixed? or if I broke anything?
Instructions to test master branch is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants