UIViewController自动旋转手动布局

时间:2019-07-28 07:03:27

标签: ios swift uiviewcontroller autolayout uikit

我有一个视图控制器VC1,它可以自动旋转到所有方向。我通过激活和停用适当的自动布局约束来手动设置接口。在自动旋转时,我会这样做:

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let orientation = UIApplication.shared.statusBarOrientation

    super.viewWillTransition(to: size, with: coordinator)



    coordinator.animate(alongsideTransition: { [unowned self] (_) in

        let orient = UIApplication.shared.statusBarOrientation


        self.layoutInterfaceForOrientation(orient)

    }, completion: { [unowned self] (UIViewControllerTransitionCoordinatorContext) -> Void in

        let orient = UIApplication.shared.statusBarOrientation

        NSLog("rotation completed to \(orient.rawValue)")
        self.layoutInterfaceForOrientation(orient)
    })

}

现在是问题所在。我有VC2,可以通过触摸按钮通过VC1的segue调用它。 VC2仅支持横向模式。因此,如果我的VC1处于纵向模式,并且segue被触发,则VC1屏幕仍处于纵向模式,但开始布局元素,就像屏幕处于横向模式一样,并且在segue过渡时,我的所有UI看起来都乱了乱码。这是因为UISection.shared.statusBarOrientation在转换过程中返回横向模式,但iPhone仍保持垂直。

我尝试了很多事情,例如在进行标记时设置标记,并在设置标记时禁用自动旋转,但没有效果。有什么想法吗?

0 个答案:

没有答案