1

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

1 回答 1

5

在您的 viewDidAppear 添加以下内容:

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

斯威夫特 3.0:

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

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

于 2016-05-07T17:10:15.833 回答