Why won't my UIViewController rotate with the device?
Q:
Why won't my UIViewController
rotate with the device?
A: The UIViewController
class provides the fundamental view-management model for iOS 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 automatic in most cases. Here is a fairly exhaustive list of reasons a view controller may not rotate.
You have added your view controller's
UIView
property toUIWindow
as a subview.Developers are discouraged from adding the view property of any view controller as a subview of
UIWindow
. Your application's root view controller should be assigned to the app window'srootViewController
property either in Interface Builder, or at runtime before returning fromapplication:didFinishLaunchingWithOptions:
. If you need to display content from more than one view controller simultaneously, you should define your own container view controller and use it as the root view controller. See Creating Custom Container View Controllers.Beginning with iOS 6, if a root view controller has not been assigned to the app's window, supported orientations are determined only by the
UIApplication
object. Further, view controllers will not be notified of orientation changes, which may result in unexpected behavior.The view controller does not support the new orientation.
Beginning with iOS 6, a view controller can limit its supported orientations by overriding this method:
- (NSUInteger)supportedInterfaceOrientations
The default implementation of
supportedInterfaceOrientations
returns a rotation mask of the recommended orientation values for the current device's idiom. That is,UIInterfaceOrientationMaskAll
is returned on iPad devices andUIInterfaceOrientationMaskAllButUpsideDown
is returned on iPhone devices. There is little need to override this method unless the content managed by the view controller must only be displayed in a subset of these orientations. If you override this method, your implementation must return a bitwise combination ofUIInterfaceOrientationMask
values.On iOS 5 and below, a view controller can support multiple orientations by overriding this method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Even if
shouldAutorotateToInterfaceOrientation
is implemented, you should make sure it returnsYES
for all the orientations you wish to support. The default implementation returnsYES
ifinterfaceOrientation
is equal toUIInterfaceOrientationPortrait
, locking the view controller into portrait orientation. To support all orientations, simply always returnYES
.The application does not support the same orientations as the view controller.
Beginning with iOS 6, UIKit uses both the
UIApplication
object and the topmost view controller to determine the supported orientations. TheUISupportedInterfaceOrientations
key in the app'sInfo.plist
must contain a value for each orientation your app supports. The system uses these values to derive the app's supported orientation mask. The application delegate may implementapplication:supportedInterfaceOrientationsForWindow:
to return aUIInterfaceOrientationMask
that is used in place of the values from the app'sInfo.plist
.On iOS 5 and below, the values for the
UISupportedInterfaceOrientations
key in the app'sInfo.plist
only serve as a hint to Springboard that it should reposition the status bar before starting your app. They do not restrict the supported orientations of your app after it has been launched.The view controller declined to rotate.
Beginning with iOS 6, when UIKit determines that rotation may need to occur, it calls the
shouldAutorotate
method of the topmost view controller to determine whether to proceed with the rotation. The default implementation of this method returnsYES
; however, a view controller may dynamically disable automatic rotation at runtime by overridingshouldAutorotate
to returnNO
.The view controller is a child of another view controller.
Beginning with iOS 6, only the topmost view controller (alongside the
UIApplication
object) participates in deciding whether to rotate in response to a change of the device's orientation. More often than not, the view controller displaying your content is a child ofUINavigationController
,UITabBarController
, or a custom container view controller. You may find yourself needing to subclass a container view controller in order to modify the values returned by itssupportedInterfaceOrientations
andshouldAutorotate
methods.The view controller overrides
init
orinitWithNibName:bundle:
but does not invoke the superclass' implementation. For the object to be initialized properly, you must call super on anyinit
orinitWithNibName
method you are overriding in your view controllers.All child view controllers in your
UITabBarController
orUINavigationController
do not agree on a common orientation set.On iOS 5 and below, to make sure that all your child view controllers rotate correctly, you must implement
shouldAutorotateToInterfaceOrientation
for 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 returnYES
for the same orientation positions.The view controller is altering the layout of its subviews programmatically.
Certain layouts are impossible to realize using autoresizing masks alone. A view controller can manually layout its subviews by altering their frame, bounds, or center properties. Developers should not place layout code in any of the rotation methods overridden by the view controller (e.g.
shouldAutorotateToInterfaceOrientation
). There are cases where the interface may rotate without these methods being called, leaving the view controller's subviews in an inconsistent state.If the app only supports iOS 5 or later, you should place layout code in the view controller's
viewWillLayoutSubviews:
orviewDidLayoutSubviews:
methods.If the app must support iOS 4, you should place layout code in the
layoutSubviews
method of the view controller's view.
Document Revision History
Date | Notes |
---|---|
2013-04-18 | Updated to include changes to autorotation in iOS 6. |
2010-07-06 | Added the case of not calling super on -(id)init. |
2010-04-08 | New document that describes situations that can prevent view controllers from rotating. |
Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-04-18