willRotateToInterfaceOrientation not being called

2020-02-06 05:24发布

问题:

I'm returning YES in my view controller's shouldAutorotateToInterfaceOrientation function, and I can see using breakpoints that YES is being returned, however the willRotateToInterfaceOrientation method isn't being called, and nor is any other rotating method. It seems like after returning YES nothing happens!

Any ideas?

Mike

回答1:

Is this views viewController a subview of some other root view controller thats not a navigation controller? if so then the call does not propagate to the subviews controller, so that might be why your view isnt rotating.



回答2:

I have a similar problem and saw Daniel's answer however I can't find any confirmation of this in the developer documentation. Not that I don't believe the answer but I don't really understand why the orientation call does not propagate.

Someone gave me a trick that works using something like this:

[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];


回答3:

One thing to look for is I found if I had UIPopoverController called in [UINavigationController viewDidAppear], then willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation are not called. It looks like UIPopoverController being modal blocks the rotation method calls.



回答4:

Yes me too. Ok, it won't get called in a sub-viewcontroller - have to pass it down. Can deal with that. And the notification idea works well except that you only get "did..." not "will..." (afaik) and anyway it's a messy solution to a problem which shouldn't be there.

My mistake was to call [super loadView] in my loadView. Not supposed to do that. When I removed [super loadView] and alloc'd the view myself willRotateToInterfaceOrientation started working.

What's really weird is that the [super loadView] was in the sub-viewcontroller and the event wasn't even reaching the top one...



回答5:

If you are not receiving callbacks on willAutoRotateToInterfaceOrientation in any view controller, add the view controller as your root view controller's child view controller.

For Eg; say self.viewController is your root view controller and childViewController is the view controller in which you want to get auto-rotation callbacks, add the following line of code;

[self.viewController addChildViewController:childViewController];

Actually, adding as the child view controller to any view controller which gets rotation call backs will work too.

Hope it helps.



回答6:

Apple documentation

At launch time, apps should always set up their interface in a portrait orientation. After the application:didFinishLaunchingWithOptions: method returns, the app uses the view controller rotation mechanism described above to rotate the views to the appropriate orientation prior to showing the window.

So if you are using a TabBarViewController be carefull to set up the selected view in the application:didFinishLaunchingWithOptions: method.