IOS6 and above automatic rotation, manual forced rotation scheme and layout adaptation

Source: Internet
Author: User

1. Layout Adaptation MethodThis article does not discuss which layout is best, using the masonry pure code layout adaptation. (Masonry bottom is AutoLayout's Nslayoutconstraint)
2. IOS Direction Enumeration Class
Three-dimensional device direction typedef ns_enum (Nsinteger, uideviceorientation) {uideviceorientationunknown, uideviceorientationportrait, Device oriented vertically, home button on the bottom uideviceorientationportraitupsidedown,//device O riented vertically, home button on the top uideviceorientationlandscapeleft,//Device oriented horizontally, hom    E button on the right uideviceorientationlandscaperight,//Device oriented horizontally, home button on the left Uideviceorientationfaceup,//Device oriented flat, face up uideviceorientationfacedown// Device oriented flat, face down};//two-dimensional interface direction typedef ns_enum (Nsinteger, uiinterfaceorientation) {Uiinterfaceorientationun    known = Uideviceorientationunknown, uiinterfaceorientationportrait = uideviceorientationportrait,    Uiinterfaceorientationportraitupsidedown = Uideviceorientationportraitupsidedown, Uiinterfaceorientationlandscapeleft = UideviceorientaTionlandscaperight, uiinterfaceorientationlandscaperight = uideviceorientationlandscapeleft};//iOS6 later introduced combined mode Typede F ns_options (Nsuinteger, uiinterfaceorientationmask) {uiinterfaceorientationmaskportrait = (1 << UIInterfaceOri entationportrait), Uiinterfaceorientationmasklandscapeleft = (1 << uiinterfaceorientationlandscapeleft), UIInt    Erfaceorientationmasklandscaperight = (1 << uiinterfaceorientationlandscaperight),    Uiinterfaceorientationmaskportraitupsidedown = (1 << uiinterfaceorientationportraitupsidedown), Uiinterfaceorientationmasklandscape = (Uiinterfaceorientationmasklandscapeleft | Uiinterfaceorientationmasklandscaperight), Uiinterfaceorientationmaskall = (uiinterfaceorientationmaskportrait | Uiinterfaceorientationmasklandscapeleft | Uiinterfaceorientationmasklandscaperight | Uiinterfaceorientationmaskportraitupsidedown), Uiinterfaceorientationmaskallbutupsidedown = ( uiinterfaceorientationmaskportrait | UiinterfaceorientationmAsklandscapeleft | Uiinterfaceorientationmasklandscaperight),};
Get device Orientation: [[Uidevice currentdevice] orientation]
Get interface direction: [[UIApplication sharedapplication] statusbarorientation]
3, IOS6 and above version page rotation setting method
Returns whether screen rotation is supported-(BOOL) shouldautorotate{    return YES;  } Returns the supported rotation direction-(Nsuinteger) supportedinterfaceorientations  {      return uiinterfaceorientationmaskall;  } Returns the screen orientation of the priority display, if not set, the default is consistent with the previous page (note that the method is only valid for Modalviewcontroller)-(uiinterfaceorientation) preferredinterfaceorientationforpresentation{      return uiinterfaceorientationlandscapeleft;  }
4. Hierarchical factors affecting the interface rotation characteristics(1) for global
The direction supported interface orientations supports in the Info.plist file.
(2) for Window
The direction supported by Supportedinterfaceorientationsforwindow in Appdelegate.
(3) for a single page
In the case of Childrenviewcontroller, it is limited by the direction shouldautorotate and supportedinterfaceorientations support in their rootviewcontroller. (Rootviewcontroller refers to the Viewcontroller set by Self.window.rootViewController)
If it is modalviewcontroller, it is limited by its own shouldautorotate and the direction of supportedinterfaceorientations support. (If the Modalviewcontroller is another wrapped rootviewcontroller, it is similar to the Childrenviewcontroller principle above)
Note: The final intersection of the three levels above is the rotation direction supported by the child View controller, and if the intersection is empty, the uiapplicationinvalidinterfaceorientationexception exception is thrown.
5. Screen rotation mechanism flow(1) The accelerometer detects direction changes and issues uideviceorientationdidchangenotification notifications.
(2) The program receives the notification, through the appdelegate to know the current program Window.
(3) Window notification Rootviewcontroller, depending on the following settings to determine whether to rotate.
    • Info.plist supported interface orientations support this direction
    • Whether this direction is supported in Supportedinterfaceorientationsforwindow in Appdelegate
    • Whether Shouldautorotate is YES in Rootviewcontroller
    • Whether Supportedinterfaceorientations supports this direction in Rootviewcontroller
(4) Rootviewcontroller notify its childrenviewcontroller, synchronous rotation operation.
In general, Childrenviewcontroller is not set individually and is consistent with Rootviewcontroller. If special scenarios need to be set individually, you can delegate permissions by delegating them in Rootviewcontroller, such as: Navigationcontroller can be delegated by Self.topviewcontroller Tabbarcontroller can be delegated by Self.selectedviewcontroller. Be aware, however, that Childrenviewcontroller must adhere to the settings in Info.plist and appdelegate even if permissions are delegated.
(5) If a pop-up Modalviewcontroller is present, it is not limited to the Rootviewcontroller in step 4, and the rotation is determined only by Info.plist, Appdelegate, and the rotation settings supported by itself. If Modalviewcontroller is another wrapped rootviewcontroller, then the principle of step 4 is similar.
6, product development in response to the strategy. (1) Applications only need to support a single direction. Locks the specified direction in the Info.plist and limits the screen rotation.
(2) Application unification supports automatic rotation in multiple directions. Set the orientation of the app support in Info.plist, with each page corresponding to the automatic rotation layout adaptation.
(3) Apply different page support direction is inconsistent. Set the info.plist of all the supported directions in the page, and in Rootviewcontroller, the permissions are set by the page but with the rotation of the control itself.
7, the actual scene application (there is a sample demo)Note: This article does not consider compatibility with the following versions of IOS6, so the following demo is only suitable for iOS6 and above (only iOS7, IOS8 tested). In the scenario below, the IPAD supports four directions by default, and the IPhone supports uiinterfaceorientationmaskportraitupsidedown three directions by default.
(1) Scenario 1: The application supports a single direction and limits rotation. (This method is generally used more often in the IPhone)
Ideas: In the Info.plist to lock the specified direction, the other rotation settings are not configured, suitable for a lot of ways, but also relatively easy, but it is recommended that the pure code or through masonry layout adaptation.
Example: Limitportraitdemo
(2) Scenario 2: Application Unified support multi-direction automatic rotation. (This method is used more often in the IPad)
Ideas: Set the application support in the Info.plist rotation direction, the other rotation settings are not configured, layout to be suitable for horizontal screen and vertical screen, pure code is recommended through masonry layout adaptation.
Example: Anyrotationdemo
(3) Scenario 3: The app supports a single direction, but individual pages support automatic rotation. (generally not recommended, unless a specific scene, such as the video player page, auto-rotation after the horizontal screen to see better results)
Idea: Set all the rotation orientations supported by the app in Info.plist, lock the specified direction through shouldautorotate and supportedinterfaceorientations in Rootviewcontroller, and then Multiple rotation orientations are set in Modalviewcontroller through Shouldautorotate and supportedinterfaceorientations, and the corresponding layout is adapted. The appropriate way of pure code is also recommended masonry.
Example: Singlerotationdemo
(4) Scenario 4: The application supports a single direction, but individual pages support manual rotation control, and automatic rotation is not supported. (generally not recommended, unless a specific scenario, such as the video player page limit automatic rotation, click the Full Screen button to view the horizontal screen)
Ideas: There are two types of forced rotation, one with the navigation bar, one is not with the navigation bar. Examples of specific implementation ideas are described in detail.
Example: Forcerotationdemo
8, the related matters needing attention
    • In general, it is not recommended to call Uideviceorientation in the program directly, but with Uiinterfaceorientation.
    • Get the screen method, do not use [[Uidevice curentdevice] orientation], it is recommended to use [[UIApplication sharedapplication] statusbarorientation].
    • Setting Setstatusbarorientation is not useful if shouldautorotate returns YES.
    • If Shouldautorotate returns no, [[Uidevice Currentdevice] Respondstoselector: @selector (setorientation:)] method does not work.
    • Try not to use forced rotation, by present out Modalviewcontroller way, individually control the rotation of each attempt to set the controller. Apple's official support is also in this way.

Copyright NOTICE: This article is the original blogger article, reproduced please declare the source: Http://blog.csdn.net/jinnchang

IOS6 and above automatic rotation, manual force rotation scheme, and layout adaptation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.