1. Monitor screen rotation direction When dealing with iOS horizontal and vertical screens, you often deal with the three enumeration types of UIDeviceOrientation, UIInterfaceOrientation, and UIInterfaceOrientationMask, which describe the screen rotation direction from different angles. 1. UIDeviceOrientation: device orientation The iOS device orientation is obtained through the iOS accelerometer. 1) iOS defines the following seven device orientations
Note: UIDeviceOrientation refers to the direction of the home button, such as: the home direction is on the right, and the device (screen) direction is on the left (UIDeviceOrientationLandscapeLeft) 2) Read the device orientation The UIDevice singleton represents the current device. From this singleton, you can get information about the device, such as the device orientation.
3) Listen, process, and remove notifications of device orientation changes When the device orientation changes, a UIDeviceOrientationDidChangeNotification notification is issued; register to listen to this notification to process view display for different device orientations.
Note: After the phone is locked in portrait orientation, the UIDeviceOrientationDidChangeNotification notification becomes invalid. 2. UIInterfaceOrientation: interface orientation The interface direction reflects the direction of the interface in iOS, which is consistent with the direction of the Home button. 1) iOS defines the following five interface directions
Note: From the definition, we can see that the interface orientation and device orientation have a corresponding relationship, such as the vertical orientation of the interface is the vertical orientation of the device: UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown 2) Read the interface direction UIInterfaceOrientation is related to the status bar and is obtained by calling statusBarOrientation through the UIApplication singleton
3) Listen, process, and remove notifications of interface orientation changes When the interface orientation changes, UIApplicationWillChangeStatusBarOrientationNotification and UIApplicationDidChangeStatusBarOrientationNotification notifications are issued successively; registering to listen to these two notifications can handle view display for different interface orientations.
Note: After the phone is locked in portrait orientation, the UIApplicationWillChangeStatusBarOrientationNotification and UIApplicationDidChangeStatusBarOrientationNotification notifications are also invalid. 3.UIInterfaceOrientationMask UIInterfaceOrientationMask is a type defined to integrate multiple UIInterfaceOrientation, related to ViewController, there are 7 types in total 1) UIInterfaceOrientationMask definition in iOS
2) Use of UIInterfaceOrientationMask In ViewController, you can override the return type of the -(UIInterfaceOrientationMask)supportedInterfaceOrientations method to determine which interface directions the UIViewController can support.
Summary: UIDeviceOrientation (device orientation) and UIInterfaceOrientation (screen orientation) are two different concepts. The former represents a state of the device, while the latter is the response of the screen on the user interface in response to different device states. When an iOS device is rotated, the rotation event is received by UIKit, and then the UIWindow object of the current program is notified through AppDelegate. The UIWindow object notifies its rootViewController. If the rootViewController supports the rotated screen orientation, the rotation is completed, otherwise it is not rotated; the same is true for the pop-up ViewController. 2. Setting the rotation direction in the view controller 0. About disabling horizontal screen operation (not recommended) There are two more conventional methods. Method 1: In the project's General–>Deployment Info–>Device Orientation, only check Portrait. Check Portrait.png Method 2: Device Orientation default settings, implement supportedInterfaceOrientationsForWindow: in AppDelegate and only return UIInterfaceOrientationMaskPortraitt (vertical screen)
Note: Very few apps have all interfaces in portrait mode, because there are always interfaces that need to support landscape mode, such as video playback pages. Therefore, it is not recommended to prohibit the app pages from being in landscape mode. Here's how to set the rotation direction in the view controller in your project: 1. APP supports multiple directions APP supports multiple directions.png Note: In this way, the APP supports both landscape and portrait orientations, but the page orientations supported by the specific view controller need further processing. Since portrait orientation (Upside Down) is not supported, even if the device is upside down, the device and screen orientation will not be obtained through the API. 2. Support ViewController screen orientation setting 1) Key functions The interface directions supported by the view controller are mainly controlled by the following three functions
2) QSBaseViewController settings
Note 1: QSBaseViewController does not support rotation by default and only supports the vertical orientation of the interface. All Controllers in the project inherit from QSBaseViewController. You can override these three methods to allow the Controller to support orientations or rotations other than vertical. 3) Set in QSNavigationController Goal: When pushing a view controller through QSNavigationController, set the screen rotation support to the view controller that is pushed most recently ([self.viewControllers lastObject]).
4) Set in QSTabBarController Goal: TabBarController is usually used as the rootViewController of the entire program. Each Tab displayed on UITabBar corresponds to a ViewController. Every time a Tab is clicked, the ViewController (self.selectedViewController) that appears controls the screen rotation and supported directions.
3. View processing under screen rotation direction 1. When the screen rotates, it is recommended to listen to UIApplicationDidChangeStatusBarOrientationNotification Reason 1: The supportedInterfaceOrientations method ultimately returns multiple interface orientations. Reason 2 (the most important reason): What we really need to deal with is the change of UI when the page direction rotates. When the physical direction of the device rotates, if the page of the current controller does not rotate at this time, we may have problems when changing the UI layout. 2. Screen width and height processing 1) After iOS 8, when the screen is rotated, [[UIScreen mainScreen] bounds] also changes. For example, the screen width in landscape mode is actually the screen height in portrait mode. 2) When we process the view layout, if we use the width and height of the screen, do not use SCREEN_HEIGHT and SCREEN_WIDTH directly, but use SCREEN_MIN and SCREEN_MAX
Note: When the screen is in portrait mode, the width is SCREEN_MIN and the height is SCREEN_MAX; when the screen is in landscape mode, the width is SCREEN_MAX and the height is SCREEN_MIN. 3. Processing Demo under screen rotation
Note: Of course, you can also choose to use an excellent AutoLayout layout third-party library such as Masonry to handle it, and storyBoard is the second best choice for layout. 4. Processing Demo effect diagram under screen rotation Vertical screen effect.png Horizontal screen effect.png 5. Suggestions for screen rotation 1) Before and after rotation, the current display position of the view should remain unchanged as much as possible 2) Response to temporary interface operations during rotation 3) If there is a tableview in the view, after rotation, force [tableview reloadData] to ensure that the new row can fill the full screen after the direction changes. 4. Force horizontal screen Some pages in the app, such as the video playback page, require horizontal screen as soon as they appear. These horizontal screen pages are either modally popped up or pushed in. 1. Setting the forced horizontal screen when the ViewController pops up modally
Note: This situation is relatively simple to handle. 2. Setting the forced horizontal screen when pushing ViewController
Note: Apple does not allow direct calls to the setOrientation method, otherwise there is a risk of being rejected; using the NSInvocation object to send a message to [UIDevice currentDevice] to force a change in the device orientation so that the page orientation changes accordingly is allowed by Apple. V. Others 1. When the APP is started, the home page UI (this page only supports vertical screen) is wrong in the horizontal screen of the mobile phone (added by 2017/6/20)
For the above detailed source code, please refer to: QSRotationScreenDemo https://github.com/buaa0300/QSKitDemo/tree/master/QSRotationScreenDemo |
<<: Android Complete Componentization Solution Practice
>>: A Brief Discussion on iOS Crash (Part 2)
This article once again focuses on a membership a...
Screenshot from the Beijing Winter Paralympic Gam...
Google said it will stop taking orders for Google ...
Your browser does not support the video tag At 22...
BYD has been having a hard time recently. BYD'...
They are both advertising channels , but why are ...
The study of Tyrannosaurus Rex and its relatives ...
Author: Xu Binbin background After a long period ...
Apple has scheduled the much-anticipated iPhone 7 ...
[51CTO.com original article] There are three comm...
As homogeneous competition in the industry become...
Lu Mingming QQ Group Precision Traffic Column 4.0...
1. The new user acquisition activities for C-end ...
The cover image is a copyrighted image. Reprintin...
Many people don’t know that ordinary-looking stat...