强制 UIWebView 的横向方向?

Force landscape orientation for UIWebView?

我有一个应用程序,其中所有视图都是纵向视图,但只有一个视图是加载 PDF 的 UIWebView 需要横向。

我的项目设置如下:

我试过以下方法:

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Landscape, UIInterfaceOrientationMask.LandscapeLeft]
    return orientation
}

上面的代码似乎不起作用。我通过在线搜索获得了代码。我不确定我做错了什么。任何建议都会有所帮助,如果您能提供一些示例代码,那就太好了。

我只想为一个 UIWebView 强制横向。

编辑:

错误:

如果此处难以阅读是错误:

支持的方向与应用程序没有共同的方向,[KM_T_World.MainViewController shouldAutorotate] 返回 YES'

在您的视图控制器中实现:

override func viewDidLoad() {
    super.viewDidLoad()
    UIViewController.attemptRotationToDeviceOrientation()
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return [UIInterfaceOrientationMask.LandscapeLeft, UIInterfaceOrientationMask.LandscapeRight]
}

使用此代码:

override func viewDidAppear(animated: Bool) {
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

您的设备方向应该是:

结果:

Sample 了解更多信息。

更新:

通过添加以下代码可以简单地锁定特定视图的方向:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask{

    return UIInterfaceOrientationMask.Portrait
}