【问题标题】:Is there a way to tell my app which UIWindow should determine the status bar orientation?有没有办法告诉我的应用程序哪个 UIWindow 应该确定状态栏的方向?
【发布时间】:2016-06-14 16:02:04
【问题描述】:

我有一个应用程序,其中主要内容呈现在一个支持所有界面方向的窗口中,而一些内容呈现在另一个仅支持纵向的窗口中。这大部分都可以正常工作,除了当设备在显示纵向窗口时旋转到横向时,状态栏仍然旋转到横向(而内容保持纵向)。

即使全方向窗口被隐藏,即使它从未被设置为关键,也是如此。这是iOS中的错误吗?我认为关键窗口应该确定状态栏的方向。

这是一个演示问题的最小应用程序:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var allOrientationsWindow: UIWindow!
    var portraitWindow: UIWindow!

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.allOrientationsWindow = createOrientationWindow(.All)
        self.allOrientationsWindow.hidden = true
        self.portraitWindow = createOrientationWindow(.Portrait)

        self.portraitWindow.makeKeyAndVisible()
        return true
    }

    func createOrientationWindow(orientations: UIInterfaceOrientationMask) -> UIWindow {
        let window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let viewController = OrientationsViewController(orientations: orientations)
        window.rootViewController = viewController
        viewController.view.backgroundColor = UIColor.whiteColor()

        return window
    }

}

class OrientationsViewController: UIViewController {

    var supportedOrientations: UIInterfaceOrientationMask

    init(orientations: UIInterfaceOrientationMask) {
        self.supportedOrientations = orientations
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        let label = UILabel(frame: CGRect(x: 50, y: 50, width: 50, height: 50))
        label.text = "Hello"
        self.view.addSubview(label)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return self.supportedOrientations
    }

    override func prefersStatusBarHidden() -> Bool {
        return false
    }

}

这是输出:

【问题讨论】:

    标签: ios screen-orientation uiwindow


    【解决方案1】:

    来自文档:

    通常,系统仅在根视图上调用此方法 controller 窗口或视图控制器,用于填充 整个屏幕;子视图控制器使用窗口的一部分 由他们的父视图控制器为他们提供,不再 直接参与有关支持哪些轮换的决策。 应用程序的方向掩码和视图的交集 控制器的方向掩码用于确定哪个 视图控制器可以旋转到的方向。

    这个控制器是否嵌入了任何东西?或者这些其他条件是否与您的情况不符?

    另外,您是否尝试在AppDelegate 中使用 - application:supportedInterfaceOrientationsForWindow:

    【讨论】:

    • 据我所知,您不能直接设置supportedInterfaceOrientations。这是你必须重写的方法,而不是属性。
    • 我明白了 - 添加答案。
    • 这两个视图控制器都被设置为它们窗口的根视图控制器,所以它们似乎都满足这个条件。这里没有说的是当有多个窗口时会发生什么。哪个窗口决定状态栏的方向?
    • 再次更新了答案,但您的通话窗口仍然可见吗?也许您可以使用 AppDelegate 方法深入研究,看看哪个被报告为根控制器? (它可能是所有旋转都ok的那个)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2015-10-13
    • 2011-09-26
    相关资源
    最近更新 更多