【问题标题】:Wrong bounds when portrait iPhone app launched on iPad with landscape orientation在 iPad 上以横向方式启动纵向 iPhone 应用程序时边界错误
【发布时间】:2021-02-07 04:16:25
【问题描述】:

我正在以编程方式创建一个没有情节提要的视图控制器。该应用仅支持“纵向设备方向(在项目常规设置中检查)”。

当应用程序以纵向启动时,它有一个正确的边界(0.0, 0.0, 375.0, 667.0);但是,当应用程序在 iPad 上以横向启动时,边界会更改为 (0.0, 0.0, 667.0, 375.0) 并以编程方式倾斜自动布局约束设置。

从下面的截图可以看出,导航栏中的标题不再居中,视图内的所有内容也是如此。

func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }

    print(">>>", windowScene.coordinateSpace.bounds)
        
    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene
    window?.rootViewController = MyViewController()
    window?.makeKeyAndVisible()
}

【问题讨论】:

  • 尝试约束布局
  • 你的意思是设置NSLayoutConstraint?你能详细说明你的评论吗?

标签: ios swift ipad autolayout uikit


【解决方案1】:

如果窗口以非纵向方向开始,则一种解决方法是切换边界宽度和高度。

func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }

    var bounds = windowScene.coordinateSpace.bounds
    let isPortrait = windowScene.interfaceOrientation.isPortrait
        
    if !isPortrait {
        let width = bounds.height
        let height = bounds.width
            
        bounds = CGRect(x: 0, y: 0, width: width, height: height)
    }

    window = UIWindow(frame: bounds)
    window?.windowScene = windowScene
    window?.rootViewController = MyViewController()
    window?.makeKeyAndVisible()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2012-03-06
    相关资源
    最近更新 更多