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

How to change UIWebView orientation programmatically in Swift

我以编程方式创建了 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)
    }
 }

在您的 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 显示时就会设置为该方向。