willRotateToInterfaceOrientation not called on iOS

2019-03-25 18:48发布

问题:

I'm using the VFR PDF viewer library in my app, where I present it thus:

ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfFile password:nil];
ReaderViewController *vc = [[ReaderViewController alloc] initWithReaderDocument:document];
[self.navigationController pushViewController:vc animated:YES];

If I run on iOS7, everything works fine.

If I run my app on iOS8, the willRotateToInterfaceOrientation method in ReaderViewController never gets called, so when the device is rotated the document doesn't get reformatted correctly.

However, if I run the demo app that comes with the library on iOS8, the willRotateToInterfaceOrientation in ReaderViewController does get called, which leads me to believe the library is ok, and I'm doing something wrong (or neglecting to do something) in my app.

I'm rather puzzled at this behaviour. Why doesn't willRotateToInterfaceOrientation get called in my app on iOS8, but it does under the other variations? How can I try to track this down?

回答1:

I finally managed to resolve my problem; here is what the issue was in case anyone else has the same problem.

My ReaderViewController was being presented from a class that had implemented viewWillTransitionToSize:withTransitionCoordinator:, so the deprecated methods on child view controllers weren't being called.

Also in that implementation it wasn't calling

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]

and so the viewWillTransitionToSize:withTransitionCoordinator: method of all child view controllers wasn't being called either.

The fix was to add a call to

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]

into the parent VC's viewWillTransitionToSize:withTransitionCoordinator: method, subclass ReaderViewController, then add a viewWillTransitionToSize:withTransitionCoordinator: to my subclass to call the appropriate methods in ReaderViewController.



回答2:

Simply because willRotateToInterfaceOrientation is no more called in iOS8, it is deprecated.

If the new rotation methods are not implemented, and a project is compiled with the iOS 8 SDK, the view controllers -will not receive calls- to the deprecated rotation methods.

A similar question to yours can be found here

Citation of @mluisbrown :

The rotation methods are deprecated in the iOS 8 SDK. This will have no effect at all on apps built with the iOS 7 SDK, even running in iOS 8 and probably several future versions of iOS.

I struggled a bit with some kind of a similar problem to yours. I tried following Apple recommendation to use viewWillTransitionToSize, which in my case did not solve my problem because this only gets triggered on changes from regular to compact for example an not on rotation.

viewWillTransitionToSize:withTransitionCoordinator: to make interface-based adjustments.

Which is detailed in apple documentation

Also a video of WWDC 2014 explains this but I can't remember which video it was. Perhaps the one on What's new in Cocoa touch or the one on View Controller Advancements in iOS 8.

EDIT

Note that in my case viewWillTransitionToSize, as explained, was not called because I wasn't changing from regular to compact so there was no size transition, strictly speaking for Apple.

The only solution I fount was to handle this manually in the viewDidLayoutSubviews of the corresponding view controller.

In my case I wanted to keep track of the top cell displayed in a tableview with autolayout. As it wasn't tracked automatically by the system, I had to check that manually and scroll manually to the adequate cell on rotation. That's why I did it that way. It's quite "heavy" but works in my case.

I would also be interested if anyone has an easier solution.