默认情况下为纵向,某些特殊屏幕如何自动旋转或迫使某些屏幕横向显示?

时间:2018-11-08 10:20:45

标签: ios swift rotation orientation

在我的应用中,默认情况下方向为纵向,但是在某些特殊屏幕上,我希望自动旋转视图控制器或将方向强制为横向。

AppDelegate.swift 中:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

// portrait by default
var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
    didSet{
        // force to portrait
        if interfaceOrientations == .portrait {
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
                                      forKey: "orientation")
        }
            // force to landscape
        else if !interfaceOrientations.contains(.portrait){
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
                                      forKey: "orientation")
        }
    }
}

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return interfaceOrientations
}

仅在纵向vc中:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = .portrait
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}

在自动旋转的vc中:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = .allButUpsideDown
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}

仅在横向vc中:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}

但它仍然存在错误...请帮助我

2 个答案:

答案 0 :(得分:0)

您应通过以下方式强制旋转:

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

您还必须启用自动旋转

override var shouldAutorotate: Bool{
    return true
}

当然,在项目设置中,您必须同时启用纵向和横向模式。

答案 1 :(得分:0)

您可以自定义您的Portrait View Controller和Landscape View Controller。像这样的LandscapeViewController:

```

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
 }

 override var shouldAutorotate: Bool {
    return true
 }

 override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeRight
 }

 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeRight
 }

```

需要注意一些要点:UIViewController不能决定自动旋转,可以使用UINavigationController来获取rotation属性的值:

override var shouldAutorotate: Bool {
    return (self.topViewController?.shouldAutorotate)!
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return (self.topViewController?.supportedInterfaceOrientations)!
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
}