Rotation---orientations for iOS----apps

Source: Internet
Author: User

This blog post is primarily for iOS apps and is a summary of issues related to screen rotation. The main contents are:

    1. ios5,6,7 different versions of the adaptation.
    2. Force rotation and automatic rotation.
    • qq:1101819159
    • Email: [Email protected]
    • Author: Magic-van.
    • Date: 2016-11-25

Three ways to change orientation

Here, let's be clear: what settings can change the screen rotation characteristics ? Such

    • We can find out the reasons for any problems in these ways.
    • Flexibility to meet the needs of product managers.

First we learned that:

    1. When the phone's gravity sensor is turned on, if the user spins the phone, the system throws the UIDeviceOrientationDidChangeNotification event.
    2. You can set Application and UIViewcontroller support the rotation direction individually. Application Settings affect the entire app, UIViewcontroller the settings only affect one viewController (IOS5 and IOS6 are different, explained in detail below).
    3. When UIKit the UIDeviceOrientationDidChangeNotification event is received, it will be based on Application UIViewcontroller the setting and if both sides support this direction, the screen will rotate to this direction. The more code expression is that the two settings will be asked, and the direction can be supported. If you ask for a with The following, there is no direction to support, it throws an UIApplicationInvalidInterfaceOrientationException exception.



Info.plist settings

In the app's Info.plist, set:

key xcode name summary avilable value
uiinterfaceorientation initial interface orientation specifies the initial orientation of the app ' s user interface. uiinterfaceorientationportrait,
Uiinterfaceorientationportraitupsidedown,
Uiinterfaceorientationlandscapeleft,
uiinterfaceorientationlandscaperight
supported interface orientations Specifies the orientations that app supports. uiinterfaceorientationportrait,
Uiinterfaceorientationportraitupsidedown,
Uiinterfaceorientationlandscapeleft,
uiinterfaceorientationlandscaperight

Once set in Info.plist, all supported auto-rotation directions in this app are only a subset of the viewController directions that the app supports.



UIViewControllerIOS6 and Abovesupportedinterfaceorientations

In IOS6 and above, the method is added UIViewController.supportedInterfaceOrientations . This method returns viewController the currently supported orientation. However, this method only takes effect in two cases:

    1. viewController window at the moment rootViewController .
    2. This is the current viewController modal pattern. That is, this viewController is presentModalViewController called and is displayed.

In both cases, the UIViewController.supportedInterfaceOrientations method acts on both the current viewController and all childViewController . The above two cases, UIKit and will not ignore your supportedInterfaceOrientations method.

Give me a chestnut:

- (NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;}

If one viewController implements the above method, this supports both the viewController vertical and the left rotation direction. viewController All of this childViewController also supports these two directions, not much more.

Preferredinterfaceorientationforpresentation

This method also belongs to UIViewController . Affects the current viewController initial display direction. This method also only viewController takes effect if it is currently rootViewController or is a modal pattern.

Shouldautorotate

This method is used to set viewController whether automatic rotation is currently supported. If you need to viewController pause auto-rotation for a little while. Then this method can be implemented. Similarly, this method only viewController takes effect if it is currently rootViewController or is a modal pattern.

IOS5 and before

In IOS5 and previous versions, each viewController can specify the direction in which it can automatically rotate. ( isn't that nice? Why did the Apple engineers make this...).
Whenever UIkit UIDeviceOrientationDidChangeNotification a message is received, the following methods are used to inquire that the currently displayed viewController branch does not support this direction:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation{   if ((orientation == UIInterfaceOrientationPortrait) ||       (orientation == UIInterfaceOrientationLandscapeLeft))      return YES;   return NO;}

In particular, it is important to note that you must return at least one Direction YES . There's always nothing good about embarrassing the system, you know.



Uiview.transform

The last method is UIView to set the transform properties to force the rotation.
See the following code:

//设置statusBar[[UIApplication sharedApplication] setStatusBarOrientation:orientation];//计算旋转角度float arch;if (orientation == UIInterfaceOrientationLandscapeLeft)    arch = -M_PI_2;else if (orientation == UIInterfaceOrientationLandscapeRight)    arch = M_PI_2;else    arch = 0;//对navigationController.view 进行强制旋转self.navigationController.view.transform = CGAffineTransformMakeRotation(arch);self.navigationController.view.bounds = UIInterfaceOrientationIsLandscape(orientation) ? CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH) : initialBounds;

It is important to note that:

    1. Of course, we can rotate the current and do it viewController for any view rotation. However, you will find that navigationBar it is still across there. So, we'd better rotate a full screen. view Here we rotate the object is self.navigationController.view , of course self.window , help yourself~
    2. We need an explicit setting bounds . UIKit I didn't know you were sneaking up on these things, so I couldn't help you set them up automatically.

How to meet the needs of the product manager

With the above three weapons, I think I can basically cope with all the needs of BT product managers. But here are some tips.

Direct locking

Slightly

With system rotation IOS5 and before

For IOS5 and previous versions, viewController shouldAutorotateToInterfaceOrientation you can easily control each direction as long as you are overriding each method viewController .

IOS6 and later

For IOS6 and later versions, if you want to conveniently control each viewController direction individually. You can use this:

    • For non- modal modal viewController :

      • If it is not rootViewController , then the override supportedInterfaceOrientations , preferredInterfaceOrientationForPresentation as well as the shouldAutorotate method, viewController returns the value of the response as currently required.
      • If rootViewController so, then override the method as follows:
-(NSUInteger)supportedInterfaceOrientations{    return self.topMostViewController.supportedInterfaceOrientations;}-(BOOL)shouldAutorotate{    return [self.topMostViewController shouldAutorotate];}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return [self.topMostViewController preferredInterfaceOrientationForPresentation];}-(UIViewController*)topMostViewController{    //找到当前正在显示的viewController并返回.}

It is obvious that we have cleverly bypassed the UIKit rules of the only method that is called. Give the rootViewController decision to what is currently being displayed viewController .

    • For modal a pattern viewController , rewrite as needed supportedInterfaceOrientations , preferredInterfaceOrientationForPresentation as well as the shouldAutorotate method.
Force rotation

Sometimes it is necessary not to rotate with the system, but to force the rotation to an angle. The most typical scenario is a video player, which needs to be displayed sideways when the full-screen button is clicked.

    • For IOS5 and previous versions, you can use the following method:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {    SEL selector = NSSelectorFromString(@"setOrientation:");    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];    [invocation setSelector:selector];    [invocation setTarget:[UIDevice currentDevice]];    int val = UIInterfaceOrientationLandscapeRight;    [invocation setArgument:&val atIndex:2];    [invocation invoke];}
    • For IOS6 and later versions. UIDevice.setOrientationchanged from hidden to removed. This can only be done by setting UIView.transform the method.

Rotation---orientations for iOS----apps

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.