IOS Orientation, how can I convert it ~~~

Source: Internet
Author: User

This blog post is mainly for IOS apps and is a summary of the problems related to screen rotation. The main content is:

  • Blog: http://www.cnblogs.com/jhzhu
  • Email: jhzhuustc@gmail.com
  • Author: Zhi Ming-so
  • Time:


Three ways to change Orientation

Here, we mainly clarify:Which of the following settings can change the screen rotation?. In this way:

  • We can find the cause of any problem through these channels.
  • Flexibly meet various requirements of product managers.

First, we need to know:



Info. plist settings

Set in Info. plist of the App:

Key Xcode name Summary Avilable value
UIInterfaceOrientation Initial interface orientation Specifies the initial orientation of the app's user interface. UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight
UISupportedInterfaceOrientations Supported interface orientations Specifies the orientations that the app supports. UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight

After setting in Info. plist, allviewControllerThe supported automatic rotation directions can only be a subset of the directions supported by the app.



UIViewControllerIOS6 and abovesupportedInterfaceOrientations

Added methods in IOS6 and later versions.UIViewController.supportedInterfaceOrientations. This method returns the currentviewControllerSupported. However, this method takes effect only in two cases:

In the above two cases,UIViewController.supportedInterfaceOrientationsThe method will apply to the currentviewControllerAnd allchildViewController. In addition to the above two cases,UIKitIt won't take care of you.supportedInterfaceOrientationsMethod.

Example:

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

IfviewControllerIf the above method is implementedviewControllerThe vertical and left rotation directions are supported.viewControllerAllchildViewControllerThese two directions are also supported, not much.

PreferredInterfaceOrientationForPresentation

This method also belongsUIViewController. Affects the currentviewControllerThe initial display direction of. This method is only available in the currentviewControllerYesrootViewControllerOrmodalMode.

ShouldAutorotate

This method is used to set the currentviewControllerWhether automatic rotation is supported. If yes, you needviewControllerPause automatic rotation for a moment. You can use this method. Similarly, this method is only available in the currentviewControllerYesrootViewControllerOrmodalMode.

IOS5 and before

In IOS5 and earlier versionsviewControllerYou can specify the direction in which you can rotate automatically .(Isn't that good? Why are Apple engineers doing this...).
Every timeUIkitYesUIDeviceOrientationDidChangeNotificationWhen a message is displayed, the following method is used to query the currently displayedviewControllerSupport does not support this direction:

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

Note: You must return at least one direction.YES. (There is always no good thing in a difficult system, you know ).



UIView. transform

The last method is to setUIViewOftransformAttribute to force rotation.
See the following code:

// Set statusBar [[UIApplication sharedApplication] setStatusBarOrientation: orientation]; // calculate the Rotation Angle float arch; if (orientation = UIInterfaceOrientationLandscapeLeft) arch =-M_PI_2; else if (orientation = UIInterfaceOrientationLandscapeRight) arch = M_PI_2; else arch = 0; // For navigationController. view to forcibly rotate self. navigationController. view. transform = CGAffineTransformMakeRotation (arch); self. navigationControl Ler. view. bounds = UIInterfaceOrientationIsLandscape (orientation )? CGRectMake (0, 0, SCREEN_HEIGHT, SCREEN_WIDTH): initialBounds;

Note that:


How to meet the requirements of product managers

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

Lock directly

(Omitted)

Rotate IOS5 with the system and before

For IOS5 and earlier versionsviewControllerRewriteshouldAutorotateToInterfaceOrientationMethod to easily control eachviewControllerDirection.

IOS6 and later

For iOS 6 and later versionsviewControllerYou can use the following method:

  • For non-modalModeviewController:

    • If notrootViewController, Then rewritesupportedInterfaceOrientations,preferredInterfaceOrientationForPresentationAndshouldAutorotateMethod, according to the currentviewControllerThe value of the response to be returned.
    • If yesrootViewController, The following rewrite method:
-(NSUInteger) supportedInterfaceOrientations {return self. topMostViewController. supportedInterfaceOrientations;}-(BOOL) shouldAutorotate {return [self. topMostViewController shouldAutorotate];}-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {return [self. topMostViewController preferredInterfaceOrientationForPresentation];}-(UIViewController *) topMostViewController {// locate the currently displayed viewController and return .}

Obviously, we cleverly bypassedUIKitCall onlyrootViewControllerRules of the method. The decision is handed over to the currently displayedviewController.

  • FormodalModeviewController. Rewrite as neededsupportedInterfaceOrientations,preferredInterfaceOrientationForPresentationAndshouldAutorotateMethod.
Forced Rotation

Sometimes, you do not need to rotate it with the system, but forcibly rotate it to a certain angle. The most typical scenario is the video player. When you click the full screen button, you need to display it horizontally.

  • For IOS5 and earlier 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 iOS 6 and later versions.UIDevice.setOrientationFrom hide to remove. You can only setUIView.transform.



References
  • How to forcibly rotate two screens in iOS
  • Supporting Multiple Interface Orientations

 

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.