简体   繁体   中英

How to change the device orientation to portrait when the current orientation is faceup or facedown

当设备的当前方向为面朝上或面朝下时,是否有可能将设备方向自动旋转为纵向?

You can use this trick to make the system refresh your controller orientation anytime you want:

[self presentViewController:[UIViewController new] animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];

This will call supportedInterfaceOrientations on your controller.

Just return UIInterfaceOrientationMaskPortrait when the device orientation is facing up or down and your controller's view will get adjusted accordingly.


Observe UIDeviceOrientationDidChangeNotification s to use the trick when you receive the notification and it's a face up/down device orientation, as those orientation do not correspond to UIInterfaceOrientation s (and won't trigger supportedInterfaceOrientations ).

Device and interface orientation definitions:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

You don't change the orientation. Instead, you tell iOS which orientations you support, so when the user rotates the device, iOS will switch the UI orientation to one that your application supports. The UI orientation should only change when the user rotates the device.

Use this code in your - (void)viewWillApper method

if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
 //do something;
}
else
{
}

You can disable or enable rotation of the screen in portrait mode by set an appropriate value in the project settings (Targets -> General -> Deployment Info section).

  • mark the Portrait checkbox
  • mark the Upside Down checkbox (this enable Upside Down animation)

or

  • mark the Portrait checkbox
  • unmark the Upside Down checkbox (this is only Upside mode)

Use self.view.window?.windowScene?.interfaceOrientation .

This value will be either portrait , portraitUpsideDown , landscapeRight , or landscapeLeft .

See here for documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM