简体   繁体   English

当前方向为面朝上或面朝下时如何将设备方向更改为纵向

[英]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). 您可以通过在项目设置(“目标”->“常规”->“部署信息”部分)中设置适当的值来禁用或启用纵向旋转屏幕。

  • 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 .使用self.view.window?.windowScene?.interfaceOrientation

This value will be either portrait , portraitUpsideDown , landscapeRight , or landscapeLeft .该值将是portraitportraitUpsideDownlandscapeRightlandscapeLeft

See here for documentation.有关文档,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设备方向为faceDown或faceUp时如何获取界面方向? - How to get the interface orientation when the device orientation is faceDown or faceUp? 在没有 .faceUp 和 .faceDown 的情况下对方向变化做出反应 - Reacting to orientation changes without .faceUp and .faceDown 当FaceUp / FaceDown时,iOS UIKeyboardWillShowNotification userInfo - iOS UIKeyboardWillShowNotification userInfo when FaceUp/FaceDown 当shouldAutorotate = NO并且仅允许纵向时,如何确定设备方向? - How to determine device orientation when shouldAutorotate = NO and only portrait is allowed? 如何更改设备方向 - How to change device orientation 当方向锁定打开且应用程序仅支持纵向时,如何检测设备方向 - How can I detect device orientation when the Orientation Lock is on and the app only supports Portrait iPhone界面方向更改未恢复为纵向 - iPhone interface orientation change not returning to portrait orientation iPad - 根据设备方向更改图像(纵向或横向) - iPad - Change image depending on device orientation (Portrait or Landscape) 通过单击 iOS 中的按钮将设备方向从纵向更改为横向 - Change the device orientation from portrait to landscape on a button click in iOS 设备以横向启动时,UIDevice方向返回纵向 - UIDevice orientation returns portrait when device launches as landscape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM