本文介绍了如何在Swift中以编程方式更改UIWebView方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created UIWebView programmatically. Now i want to set programmatically its orientation to (landscape or portrait).Please can anyone tell me how i can do this?Thanks

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中添加以下内容:

In your viewDidAppear add the following:

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时它将被设置为它。

Just choose your orientation and it will be set to it when the WebView is shown.

这篇关于如何在Swift中以编程方式更改UIWebView方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:06