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

时间:2017-05-18 10:07:24

标签: ios swift xcode webview orientation

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

导入UIKit 导入WebKit

类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进行哪些更改。

2 个答案:

答案 0 :(得分:3)

我得到了答案 -

  1. 如果您只想旋转webview,请使用 -
  2. myWebView?.transform = CGAffineTransform(rotationAngle:CGFloat(Double.pi / 2))

    1. 如果您想在横向模式下使用视图控制器 -
    2. 让value = UIInterfaceOrientation.landscapeRight.rawValue         UIDevice.current.setValue(value,forKey:" orientation")

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

答案 1 :(得分:0)

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

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

让所有代码保持不变。