【问题标题】:How to lock orientation of one view controller to landscape mode [iOS13]如何将一个视图控制器的方向锁定为横向模式 [iOS13]
【发布时间】:2019-11-07 16:23:46
【问题描述】:

我有这个解决方案: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 和 iPhone X。 => we.tl/t-I3Um0U43fV

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

有人知道如何避免吗?

【问题讨论】:

    标签: uiviewcontroller orientation ios13 xcode11 swift5


    【解决方案1】:

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

    class ViewController: UIViewController {
    
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
      }
    
      // rest of your code
    }
    

    编辑: 根据示例项目,您需要选择退出新的演示样式以获得与 iOS 13 上相同的行为。在您以代码呈现的视图控制器上设置 modalPresentationStyle = .fullScreen 或选择相应的选项界面生成器。

    【讨论】:

    • 这是第二个链接。它在 iOS 12 上完美运行,但在 iOS 13 上却不行 :( 我将使用示例应用程序编辑我的问题。
    • 谢谢,现在才找到。它可以工作,但对我来说,主屏幕在过渡期间在屏幕的顶部和底部都有黑色部分。你知道如何避免吗?
    【解决方案2】:

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

    在 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;}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多