强制UIWebView的横向方向?

时间:2015-09-29 10:14:45

标签: ios swift uiwebview swift2 landscape

我有一个应用程序,其中所有视图都是肖像,但只有一个视图是加载PDF的UIWebView需要在Landscape中。

我的项目设置如下:

enter image description here

我尝试了以下内容:

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

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

以上代码似乎不起作用。我通过在线搜索得到了代码。我不确定我做错了什么。任何建议都会有所帮助,如果你能提供一些示例代码就会很棒。

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

修改

enter image description here

错误:

enter image description here

如果这里难以阅读的是错误:

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

2 个答案:

答案 0 :(得分:2)

在View Controller中实现:

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

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

settings info

答案 1 :(得分:0)

使用此代码:

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

您的设备方向应为:

enter image description here

结果:

enter image description here

Sample了解更多信息。

<强>更新

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask{

    return UIInterfaceOrientationMask.Portrait
}