Painted levels: being fostered fostered fostered

Interface rotation in iOS by Dac_1033 Review: QiShare team


There are several requirements for video playback in the project I have contacted recently. In this project, the video player can be manually switched between full-screen/portrait screen or automatically switched, but other interfaces are displayed in portrait screen. So, here’s a summary of some of the things iOS has to do with interface rotation, i.e. landscape/portrait switching.

Note: There is no explicit way to set the orientation of the interface in iOS.

1. View rotation

If the need is to rotate only one view, we can simply call the following methods to manually rotate the view.

view.transform = CGAffineTransformMakeRotation(M_PI_2);
Copy the code

If you want to rotate the entire current screen of your app to landscape in this way, you may also need the following methods:

/ / the status bar to rotate the [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeLeft animated: NO]; // Force device orientation to set keyboard eject orientation (note: But this is a private method) [[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];Copy the code

2. Use the system method to achieve interface rotation

Automatic rotation of the interface is realized by the system method. First of all, you need to check the options in the project:

Second, implement the following methods in an AppDelegate:

/ / the whole app allows automatic rotating direction - (UIInterfaceOrientationMask) application: (UIApplication *) application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }Copy the code

Finally, implement the system method in the corresponding ViewController:

- (BOOL)shouldAutorotate; The direction of rotation of the / / support - UIInterfaceOrientationMask) supportedInterfaceOrientations; / / modal view initialization when the direction of rotation - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation;Copy the code

Screen rotation is determined from the full-screen view approach, the root view. That is, every time you decide if the page needs to rotate, you’re going to do it from the nav on the left. To customize each page, we need to implement the following methods in UINavigationController and UITabbarController respectively:

/ / / / in the base class of NavgationController paste the following code - (BOOL) shouldAutorotate {return self. TopViewController. ShouldAutorotate; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; } / / / / in the base class of TabbarController paste the following code - (BOOL) shouldAutorotate {return self. SelectedViewController. ShouldAutorotate; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return self.selectedViewController.supportedInterfaceOrientations; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.selectedViewController.preferredInterfaceOrientationForPresentation; }Copy the code

3. Obtain and monitor the direction of the device

  • Obtaining device Direction
// Get device orientation [[UIApplication sharedApplication] statusBarOrientation]; [[UIDevice currentDevice] orientation];Copy the code
  • Add notifications to listen for device rotation
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; - (void)deviceOrientationChange:(NSNotification *)notification { UIDeviceOrientation orient = [UIDevice currentDevice].orientation; switch (orient) { case UIDeviceOrientationPortrait: break; case UIDeviceOrientationLandscapeLeft: break; case UIDeviceOrientationPortraitUpsideDown: break; case UIDeviceOrientationLandscapeRight: break; default: break; } // Add a notification to listen for interface rotation [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];  - (void)statusBarOrientationChange:(NSNotification *)notification { UIInterfaceOrientation orientation = [[UIApplication  sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeRight) { } if (orientation ==UIInterfaceOrientationLandscapeLeft) { } if (orientation == UIInterfaceOrientationPortrait){ } if (orientation == UIInterfaceOrientationPortraitUpsideDown){ } }Copy the code

4. Application examples of interface rotation

As mentioned in the beginning, if the app has a multi-layer interface such as QiTabBarController, UINavigationController, UIViewController, etc., and we only want one interface to rotate automatically, how can we do that? (1) Base class UITabBarController, QiNavigationController to achieve the above 👆 code; (2) The base QiViewController method shouldAutorotate returns NO; (3) The method shouldAutorotate inherited from the child controller of QiViewController returns YES to implement the above function.

5. Force an interface to rotate

Before iOS3, it was possible to implement setOrientation for UIDevice, but since then it has become private and cannot be called directly. We use KVO mechanism to call indirectly, and we are lucky if we can submit and publish to AppStore successfully!

/ / / / the most direct [[UIDevice currentDevice] setValue: @ (UIInterfaceOrientationPortrait) forKey: @ "orientation"]. //// implicit - (void)forceOrientation {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 = UIInterfaceOrientationLandscapeLeft; // Invocation invocation [Invocation invocation :&val atIndex:2]; [invocation invoke]; }}Copy the code

Project source GitHub address


Recommended articles:

Constraints on iOS layout options: Frame iOS Autoresizing iOS Layout options: StackView iOS layout options: navigation iOS UIButton automatically layouts based on content iOS specified initialization method hitTest method in UIView