Q: Why won't my UIViewController rotate with the device?
UIViewController rotate
with the device?
There can be several possible reasons your view controller does not rotate.
The UIViewController class provides the fundamental view-management model for iPhone applications. It provides automatic support for rotating
the views of the view controller in response to changes to the orientation of the device. If the autoresizing properties of your view and subviews are properly configured, this behavior is essentially free. Your view controller may not rotate in certain situations
if:
-
The view controller does not implement this delegate method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;Even if
shouldAutorotateToInterfaceOrientationis implemented, you should make sure it returns YES for all the orientations you wish to support. To support all orientations, simply always return YES. -
The view controller's
UIViewproperty is embedded insideUIWindowbut alongside an additional view controller.You may find a situation where
shouldAutorotateToInterfaceOrientationis called once at startup for a given view controller but is never called again when the device is rotated. Because view controllers are tightly bound to the views they manage, they are also part of the responder chain used to handle events. View controllers are themselves descendants of theUIResponderclass and are inserted into the responder chain between the managed view and its superview. So it is common practice to have one primary view controller in your application as part of the responder chain. You would typically add one primary view controller such as aUINavigationController,UITabBarControlleror a genericUIViewControllerto yourUIWindow. For example, this is done by calling:[myWindow addSubview:primaryViewController.view];If you add an additional view controller's
UIViewproperty toUIWindow(at the same level as your primary view controller) via the following:[myWindow addSubview:anotherController.view];this additional view controller will not receive rotation events and will never rotate. Only the first view controller added to
UIWindowwill rotate. -
You have added your view controller's
UIViewproperty toUIWindowas a subview, but prematurely released it soon after.UIWindowwill retain that view, but not the view controller itself. You must not release it prematurely. Declare it as a "retainable" property in yourUIApplicationDelegatesubclass. -
All child view controllers in your
UITabBarControllerorUINavigationControllerdo not agree on a common orientation set.To make sure that all your child view controllers rotate correctly, you must implement
shouldAutorotateToInterfaceOrientationfor each view controller representing each tab or navigation level. Each must agree on the same orientation for that rotate to occur. That is, they all should return YES for the same orientation positions. -
Overriding the
-(id)init:or-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundlemethod without callingsuper. For the object to be initialized properly, you must call super on anyinitorinitWithNibNamemethod you are overriding for your view controllers.
本文详细解释了UIViewController在iOS应用中无法随设备旋转的原因及解决方法,包括但不限于未实现shouldAutorotateToInterfaceOrientation方法、额外视图控制器嵌套、过早释放窗口视图控制器等问题,并提供了针对性的解决方案。
3317

被折叠的 条评论
为什么被折叠?



