The child view does not rotate from landscape to portrait and vice versa

社会主义新天地 提交于 2019-12-11 05:02:46

问题


I'm making an application for the iPad. I have a view controll that has to stay in landscape, this works just fine.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{
   return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

From this view i push towards an other view, this child view should rotate to the appropriate rotation (landscape or portrait) whenever the user tilts his ipad. in this child view i use this code to make this happen, but it doesn't work. It sticks to the landscape.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return YES;
}

回答1:


In order for a view controller to support rotation, all view controllers in its hierarchy must support rotation.

From the Responding to Orientation Changes documentation:

The window looks for an appropriate view controller and calls itsshouldAutorotateToInterfaceOrientation:method to determine if it supports the new orientation.

Container view controllers may intercept this method and use their own heuristics to determine whether the orientation change should occur. For example, the tab bar controller allows orientation changes only if all of its managed view controllers support the new orientation.

Further, you should not be using multiple view controllers to manage a single screen.

From the View Controller Programming Guide documentation (emphasis mine):

The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple content view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single content view controller object to manage multiple screens’ worth of content.

In this case, I'd suggest disabling rotation handling in your parent view controller, changing the child view controller to be simply a view (to meet the above criteria), and manually monitoring orientation changes to update your child view's layout.

You can monitor for orientation changes by listening for the UIDeviceOrientationDidChangeNotification notification. Example code:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(yourSelector:)
    name:UIDeviceOrientationDidChangeNotification object:nil];

Update

If by "push" you mean push a view controller onto a navigation view controller then please disregard the second part of my response.

If this is the case, you must ensure that you have overridden the shouldAutorotateToInterfaceOrientation: method of your navigation controller to return YES as well as your view controller in order to support rotation handling.



来源:https://stackoverflow.com/questions/9783554/the-child-view-does-not-rotate-from-landscape-to-portrait-and-vice-versa

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