将应用程序转换为IPAD时如何保持横向或纵向模式?

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

标签: ios swift ipad viewcontroller landscape-portrait

我目前正在使iphone应用程序适应ipad,并在viewController中实现了以下代码,以保持其中一个视图为横向(其他视图为纵向):

FList.Clear();
// then add objects to it again, such as FList.AddRange(...)

这对于iPhone来说效果很好,但是在iPad上显示应用程序时不起作用,即视图旋转并且可以纵向和横向查看。感谢您提出有关如何修改代码以使其在iPad上运行的任何建议。

1 个答案:

答案 0 :(得分:0)

添加方法viewWillTransition(to:with :)。 通知容器其视图的大小即将更改。UIKit在更改呈现的视图控制器的视图的大小之前调用此方法。您可以在自己的对象中覆盖此方法,并将其用于执行与尺寸更改有关的其他任务。

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
        // Update frame
    } else {
        print("Portrait")
         // Update frame
    }
}

enter image description here

相关问题