如何在swift中的landscape viewcontroller中显示webview?

时间:2016-06-14 09:37:40

标签: ios swift

我的viewcontroller只包含webview。但是,此webview的内容非常广泛,因此我需要在landscape模式下显示此内容。

问题是如何旋转webview或整个视图控制器?

我尝试像这样更改orientations但是没有工作:

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return .LandscapeLeft
}

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

3 个答案:

答案 0 :(得分:0)

AppDelegate中的

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Landscape
}

答案 1 :(得分:0)

如果您只想锁定该ONE页面的方向,请执行以下操作:

将此添加到您希望仅在横向显示的页面上的ViewDidLoad()。

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appdelegate.shouldRotate = false

            UIDevice.currentDevice().setValue(UIInterfaceOrientation.Landscape.rawValue, forKey: "orientation")

在班级中添加以下功能。

    override func shouldAutorotate() -> Bool {
        // Lock autorotate
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.Landscape]
    }


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        // Only allow Portrait
        return UIInterfaceOrientation.Landscape
    }

当您离开页面时,请通过添加以下代码将其解锁:

   override func viewWillDisappear(animated: Bool) {
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldRotate = true
    }

在AppDelegate中,输入以下代码:

var shouldRotate = true

    //MARK: - Func to rotate only one view controller
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if (shouldRotate == true){
            return UIInterfaceOrientationMask.All
        }

        return UIInterfaceOrientationMask.Landscape

    }

答案 2 :(得分:0)

完美运作:

将其放入viewDidLoad

    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

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