On the direction of IOS screen


General applications only support one direction of the vertical screen, but few applications support multiple screen directions.

However, in my work, I have a lot of contact with this screen direction, because we are an SDK with an interface. In order to let the access party access it, we have little experience at the beginning. Considering that the access party itself may have multiple screen directions, so we directly support four directions, and then there are various screen turning problems, 90 degree rotation, 180 reading rotation, 270 degree rotation Rotate, the test hand is almost broken.

Later, I realized that it was unnecessary and wasted a lot of time to solve the problem of screen direction. Later, it was simplified to let the access party directly set up to support a certain direction.

General applications don’t need to be so complicated, as long as they support one or two screen directions. I also make a few summary about the screen direction, hoping to help some developers!

System screen direction enumeration

By viewing the documents, the enumeration used to control the screen direction of the system is as follows:

//Enumeration used to control screen orientation before IOS 6
typedef enum {
  UIInterfaceOrientationPortrait      = UIDeviceOrientationPortrait,
  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  UIInterfaceOrientationLandscapeLeft   = UIDeviceOrientationLandscapeRight,
  UIInterfaceOrientationLandscapeRight   = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

//Enumeration used to control screen direction in IOS 6 and later
typedef enum {
  UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
  UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
  UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
  UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
  UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
  UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
   UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
  UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
} UIInterfaceOrientationMask;

It can be found that:

  • The uiinterfaceorientationmask type used by IOS 6 and later versions controls the screen direction. Several enumeration values have been added to this type. One enumeration value can be used to represent multiple screen directions.
  • In the four basic screen directions (up, down, left and right), uiinterfaceorientation mask = (1 < < uiinterfaceorientation), so if you need to dynamically convert uiinterfaceorientation type to uiinterfaceorientation mask type in your application, you just need to do the above conversion, and you don’t need to judge and re convert through switch.

How to control the screen direction

In the application of IOS, there are many ways to control the screen direction of the interface, including global, UI window and single interface.

Single interface control

Before iOS 6

Before IOS 6, this method in uiviewcontroller class was used for screen orientation control of a single interface:

//Support rotation to a certain screen direction
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  return ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) |
       (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft));

By default, the return value of this method is true only when the parameter is uiinterfaceorientationportrait, that is, only vertical screen up is supported by default. In the above example, it means that the horizontal screen is supported in two directions: right and left.

IOS 6 and later

In IOS 6 and later versions, there are two new ways to use uiviewcontroller to control the screen orientation of a single interface in IOS 6.0:

//Support screen changing
- (BOOL)shouldAutorotate
  return YES;

//Supported screen orientation, where uiinterfaceorientationmask type can be directly returned
//Multiple uiinterfaceorientationmask values can also be returned
- (NSUInteger)supportedInterfaceOrientations
  return UIInterfaceOrientationMaskLandscape;

Where – the default value of the supportedinterfaceorientations method in iPad is uiinterfaceorientationmaskall, which means all screen directions are supported by default; the default value of iPhone and iPod touch is uiinterfaceorientationmaskallbuttupsidedown, which means three directions except vertical screen down are supported.

When the device screen rotates, the system will call the – shouldauutorotate method to check whether the current interface supports rotation. Only when – shouldauutorotate returns yes, the – supportedinterfaceorientations method will be called to determine whether the interface needs rotation.

UI control in UIWindow (IOS 6 and above is valid)

In IOS 6, the uiapplicationdelegate protocol adds a method to specify the screen direction of the UI window interface:

- (NSUInteger)application:(UIApplication *)application
    supportedInterfaceOrientationsForWindow:(UIWindow *)window
  return UIInterfaceOrientationMaskLandscape;

The default value for this method is the value of the supported interface orientations entry configured in info.plist.

Generally, we will not create any other UIWindow, so through this method, we can also achieve global control.

Global control

In the info.plist file of the application, there is a supported interface orientations configuration. You can configure the screen direction of the entire application, as shown in the following figure:

This configuration is actually consistent with the supported interface orientations configuration in the summary interface of the target in the project. If you modify either side, the other side will be modified synchronously.

In addition, when the application is started, the first value in the supported interface orientations item in info.plist is used as the screen direction for starting the animation. According to the value of the screenshot here, the first value is portrait (top home button), that is, the opposite direction of the vertical screen, so when this application is started, it will use the opposite direction of the vertical screen to display the start animation.

Rules for coexistence of multiple controls

  • The last supported screen direction of an interface is the intersection of (global control ∩ UI window control ∩ single interface control). If global control supports all screen directions, UI window interface control supports horizontal screen. If only horizontal screen is supported to the right in an interface, then the final interface will only be displayed with horizontal screen to the right, and rotation to other directions is not supported.
  • If the last intersection of the screen directions supported by the above three controls is empty, IOS 5 is a little different from IOS 6. Under IOS 6, an exception of uiapplicationinvalidinterfaceorientationexception will even be thrown directly, and then it will crash directly, so the intersection of the three values should be kept non empty.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

React can be dragged to zoom in and out. The pop-up window component is ready to use

preface According to the requirements, a drag and maximize pop-up window component is made, and the effect is as follows:The magnification effect is as follows:No nonsense. Firstly, the width, height and initial window coordinates of the current page are obtained, and they are horizontally and vertically centered InitWindow=()=>{ //The initialization window is in the middle […]