方向更改后,iOS ViewController无法正确布局

时间:2015-09-16 14:39:18

标签: ios iphone swift rotation orientation

我正在编写一个iOS应用程序,其中包含一些固定(纵向)视图和一些与方向相关的视图(纵向,左侧和右侧景观)。

视图包含在自定义导航控制器中,基于以下代码:

class CustomNavigationViewController: UINavigationController {
    override func supportedInterfaceOrientations() -> Int {
        return self.topViewController.supportedInterfaceOrientations()
    }

    override func shouldAutorotate() -> Bool {
        return self.topViewController.shouldAutorotate()
    }
}

我正在导航子控制器中实现supportedInterfaceOrientations()shouldAutorotate()

这是一个测试控制器

class TestViewController: UIViewController {
    var shouldRotate: Bool = false

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.shouldRotate = true
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    convenience init(rotation shouldRotate: Bool) {
        self.init(nibName: "TestViewController", bundle: nil)
        self.shouldRotate = shouldRotate
    }

    override func supportedInterfaceOrientations() -> Int {
        if shouldRotate == false {
            return UIInterfaceOrientation.Portrait.rawValue
        } else {
            return super.supportedInterfaceOrientations()
        }
    }

    override func shouldAutorotate() -> Bool {
        return shouldRotate
    }

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
        println("from: \(fromInterfaceOrientation.rawValue)")
    }

    @IBAction func buttonTapped(sender: AnyObject) {
        let newvc = TestViewController(rotation: true)
        self.navigationController?.pushViewController(newvc, animated: true)
    }
}

第一个控制器在AppDelegaterotation: false实例化。其他控制器使用rotation: true创建,并在点击按钮后按下。

以下是该视图的屏幕截图:

first controller

如果我在第一个控制器(可旋转的控制器)之后改变控制器的方向,我会得到以下结果:

rotated controller

控制器xib使用autolayout和如果我在点击按钮之前旋转设备它按预期工作,视图填满整个屏幕。

此外,如果我在手机处于横向状态时轻按后退按钮,则第一个视图将锁定在此状态:

enter image description here

我正在将应用程序部署到iOS 8。

如何在旋转后才能制作第一张视图肖像并正确布局其他视图?

2 个答案:

答案 0 :(得分:2)

它可能并不重要,因为它只打印出调试消息,但不要忘记{8}在{8}中被didRotateFromInterfaceOrientation(_:)折旧了{{1}将它视为视图大小变化(包括旋转)的责任。

我认为您已走上正轨,但不会覆盖viewWillTransitionToSize(_:withTransitionCoordinator:)shouldAutorotate(),而只会覆盖supportedInterfaceOrientations()

我刚做了一个快速的POC,看看它是否有效。我在创建视图控制器(并使用Swift 2)时没有设置标志,但你明白了这一点:

supportedInterfaceOrientations()

我认为您的演示中发生的事情是,当您转换到已修复的视图控制器时,您会要求它进入纵向方向(就是这样)但是然后不允许屏幕旋转回纵向模式。

所以..只是尝试删除你的'shouldAutorotate()'在class RotatingViewController: UIViewController { override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.All } } class FixedViewController: UIViewController { override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } } class NavigationController: UINavigationController { override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return self.topViewController?.supportedInterfaceOrientations() ?? UIInterfaceOrientationMask.All } } 中查看是否有效。

答案 1 :(得分:0)

问题是因为shouldAutorotate返回false。 您的FixedViewController应返回shouldAutorotatetrue,但supportedInterfaceOrientations应仅为纵向。

你为什么需要它? 当您从横向模式下的另一个RotateViewController返回(通过使用popViewControllerdismissViewController)到您的FixedViewController(仅支持纵向模式)时,它应该自动旋转,以回到纵向模式