【问题标题】:How to change UIWebView orientation programmatically in Swift如何在 Swift 中以编程方式更改 UIWebView 方向
【发布时间】:2016-05-07 08:28:53
【问题描述】:

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

【问题讨论】:

  • 谢谢,但我只想改变 UIWebView 的方向而不是整个 ViewController 类。因为我必须在 CollectionVIew 类中为单元格使用 UIWebView。

标签: ios swift uiwebview swift3


【解决方案1】:

在您的 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 显示时它就会被设置。

【讨论】:

  • 我只想改变 UIWebView 的方向而不是整个 ViewController 类的方向。因为我必须在 CollectionVIew 类中为单元格使用 UIWebView。当我在我的类中添加该代码后第一次运行我的项目时,它以横向显示,但是当我使用命令和向右箭头将其更改为纵向时。然后再次运行我的项目,它不会以横向模式显示?
  • 我认为不可能实现您想要做的事情。正如我在示例中所示,您只能更改整个视图的方向。如果您需要进一步的帮助,请告诉我。
  • 例如在这个项目中,我只想在加载时将 UIWebView 的方向更改为横向,当我退出 UIWebView 时,collectionView 以纵向显示? drive.google.com/open?id=0B29ka7gbDAYxLTJWVXJTS3FQSFU
  • 只需将 webView 移动到新的 viewController 并以这种方式控制它。我使用该功能更新了您的项目。在此链接dropbox.com/s/hvoeddliwstzn7q/videoPlay.zip?dl=0 下载新版本
  • 非常感谢 Rashwan L。现在它可以按我的意愿工作了。我用这个 cell.playBt.addTarget(self, action: Selector("webView: "), forControlEvents: UIControlEvents.TouchUpInside) 和作品 (y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
相关资源
最近更新 更多