[英]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. 这将在控制器上调用supportedInterfaceOrientations
。
Just return UIInterfaceOrientationMaskPortrait
when the device orientation is facing up or down and your controller's view will get adjusted accordingly. 当设备方向朝上或朝下时,只需返回UIInterfaceOrientationMaskPortrait
,并且控制器的视图将进行相应调整。
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
). 观察UIDeviceOrientationDidChangeNotification
可以在收到通知时使用该技巧,它是设备朝上/朝下的设备方向,因为这些方向与UIInterfaceOrientation
不对应(并且不会触发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. 相反,您告诉iOS支持的方向,因此当用户旋转设备时,iOS会将UI方向切换为应用程序支持的方向。 The UI orientation should only change when the user rotates the device. 仅当用户旋转设备时,UI方向才应更改。
Use this code in your - (void)viewWillApper method 在-(void)viewWillApper方法中使用此代码
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). 您可以通过在项目设置(“目标”->“常规”->“部署信息”部分)中设置适当的值来禁用或启用纵向旋转屏幕。
or 要么
Use self.view.window?.windowScene?.interfaceOrientation
.使用self.view.window?.windowScene?.interfaceOrientation
。
This value will be either portrait
, portraitUpsideDown
, landscapeRight
, or landscapeLeft
.该值将是portrait
、 portraitUpsideDown
、 landscapeRight
或landscapeLeft
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.