如何在 swift3 中以横向模式强制调用 webview ios

How to forcefully Call webview in landscape mode in swift3 ios

我希望我的一个包含 webview 的视图控制器每次都以横向模式出现。我写过这样的代码

导入 UIKit 导入 WebKit

class DrawWebViewController: UIViewController{

@IBOutlet var scrollView: UIScrollView!

@IBOutlet var webView: UIWebView!

// var webAView: WKWebView?

override func loadView() {
    super.loadView()
   // resizeWebView()

}

/* func resizeWebView(){

    webView.scalesPageToFit = true
    webView.contentMode = UIViewContentMode.scaleToFill
    webView.isMultipleTouchEnabled = true
    webView.autoresizingMask = UIViewAutoresizing.flexibleWidth
    self.view.addSubview(webView)

}*/

    var newUrlVal = "http://wonderhomes.co/draw.php?id=bedroom"
override func viewDidLoad() {
    super.viewDidLoad()

    if let url = URL (string: newUrlVal){
        let requestObj = URLRequest(url: url)
        _ = webView.loadRequest(requestObj)

        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }

    //Image for logo at navigation bar
    let logo = UIImage(named: "logo-250-by-60---1.png")
    let imageView = UIImageView(image:logo)
    self.navigationItem.titleView = imageView

    // Do any additional setup after loading the view.
}
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeLeft
}
private func shouldAutorotate() -> Bool {
    return false
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

任何人都可以建议我可以进行哪些更改以在横向模式下调用 webview。

只需在 ViewController 的 viewDidAppear 方法中调用以下方法即可。

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

并让所有代码保持不变。

我得到了答案 -

  1. 如果你只想旋转 webview 然后使用 -

myWebView?.transform=CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))

  1. 如果你想在横向模式下查看控制器 -

设值=UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(value, forKey: "orientation")

把上面这行写在你想要的地方。