如何将一个视图控制器的方向锁定为横向模式[iOS13]

时间:2019-11-07 16:23:46

标签: uiviewcontroller orientation ios13 xcode11 swift5

我有以下解决方案:How to lock orientation of one view controller to portrait mode only in Swift

它运行正常,但现在在iOS 13上不再运行。

我尝试以下解决方案:Screen rotation glitch on iPadOS 13

但是它不能解决问题。

有人可以为肖像应用程序提供一个可行的iPhone应用程序解决方案,而在横向视图中只有一个UIViewController吗?

编辑: 我制作了一个示例应用程序,它可以在iOS 12.4.2和iPhone 6上正常工作。 但是我的问题是它不适用于iOS 13.1.3和iPhoneX。 => we.tl/t-I3Um0U43fV

编辑2: 感谢@donnywals,我成功地保持了与iOS 12相同的方向。 我只需要一些帮助即可在过渡期间清洁主屏幕。 “ TOP”标签距超级视图顶部90px。如您所见,当前的纵向页面在过渡期间会调整大小。

有人知道如何避免吗?

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以通过覆盖视图控制器的supportedInterfaceOrientations属性来锁定其方向:

class ViewController: UIViewController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
  }

  // rest of your code
}

编辑: 根据示例项目,您需要选择退出新的演示文稿样式,以实现与iOS 13相同的行为。在代码中显示的视图控制器上设置modalPresentationStyle = .fullScreen或选择相应的代码接口构建器中的选项。

答案 1 :(得分:0)

这是一个经过测试的代码,我已经实现了相同的代码并且它对我有用 - 强制控制器进入横向模式

在方法 viewWillAppear 中添加以下内容:

NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

在视图控制器中添加以下方法

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}

- (BOOL)shouldAutorotate {
return true;}
相关问题