iOS: Orientation check rotation on ViewDidAppear

不羁的心 提交于 2020-01-23 09:46:36

问题


Background: I want to make sure my viewControllers rotate properly when it appears. My viewControllers have excellent codes managing the rotation and orientation when it is visible.

Problem: Given two viewControllers in a NavigationController, viewC1 and viewC2. I did the following:

1.) Set rootViewController to viewC1

2.) Push viewC2 into the NavigationController

3.) Rotate

4.) Pop viewC2

5.) viewC1 is still stucked in the old orientation look (as in the transformation code in willAnimateRotationToInterfaceOrientation was not called) with the new orientation.

What can I do to ensure viewC1 call willAnimateRotationToInterfaceOrientation to reconstruct itself to look correctly in the new rotation?

Additional info: This is all code (no storyboard/xib). I have shouldAutorotateToInterfaceOrientation return YES on all the views. I use willAnimateRotationToInterfaceOrientation to manage all my rotation.

Oh, and please no hacks. For example, copy the code from rotation then check the rotation mannually and manage it in viewDidAppear.


回答1:


Think about the name of the method, and what you're trying to achieve.

willAnimateRotationToInterfaceOrientation indicates that the view controlled by the view controller is about to animate to a particular orientation. If your view is in the middle of a navigation stack, then it is not being displayed on screen. To animate something that isn't on screen is costly and ultimately worthless. So, that particular method is out of the question, but the problem that remains is there isn't anything else more appropriate in UIKit. The reason is to rotate something (even if not animated) when it's offscreen is worthless cost. It's the responsibility of the developer to handle a change in orientation when the view appears ("transformation on demand" as you will).

You say you don't want hacks, but the method you've described as a hack is probably the most appropriate thing to do. Having a generic method named something like -(void) updateLayoutForOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated { ... } isn't a bad idea. This can be the handler for orientation change transformations for the whole view controller.

The places you need to possibly check/handle orientation issues are

-(void) viewWillAppear:(BOOL)animated

-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration

and in both of these, call updateLayoutForOrientation:animated: to do the work for you.



来源:https://stackoverflow.com/questions/10837294/ios-orientation-check-rotation-on-viewdidappear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!