如何在Swift中以编程方式更改UIWebView方向

时间:2016-05-07 08:28:53

标签: ios swift uiwebview swift3

我已经以编程方式创建了UIWebView。现在我想以编程方式设置其方向(横向或纵向)。 请有人能告诉我怎么做吗? 感谢

import UIKit
class ViewController: UIViewController, UIWebViewDelegate  {

    override func viewDidLoad() {
        super.viewDidLoad()

        let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://tune.tv/humtv-live-embed")!))
        webV.delegate = self;
        self.view.addSubview(webV)
    }
 }

1 个答案:

答案 0 :(得分:5)

在viewDidAppear中添加以下内容:

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

Swift 3.0:

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

只需选择您的方向,当显示WebView时它将被设置为它。