Swift Webview力量景观

时间:2016-07-28 09:13:07

标签: ios swift xcode orientation

我使用Swift在xCode中创建一个webview。现在我想要一些屏幕力旋转到横向的页面。还有一些页面自动定位。我怎么能这样做?

更新

我在Reinier的回答后做了这个:

func loadAddressURL(){
    let requestURL = NSURL(string: URLPath)
    let request = NSURLRequest(URL: requestURL!)

    //url name another will show in landscape mode
    if(request.URL?.absoluteString == baseUrl + "dashboard")
    {
        print(request.description)

        if(UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
        {
            let value = UIInterfaceOrientation.LandscapeRight.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")

        }
    }

    WebView.loadRequest(request)

但它不起作用。当我将设备变为肖像时,应用程序会旋转为纵向,但我想强制横向显示。

1 个答案:

答案 0 :(得分:2)

我一直在研究你的问题,这是我的结果,首先你需要在加载新URL的事件发生时通知这个方法你可以实现这个,当然你需要设置为委托你的webView查看你的viewController,然后实现这个方法

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool

这是代码

已更新

要保持这种方向,您需要实现此方法func supportedInterfaceOrientations() -> UIInterfaceOrientationMask并在一个变量中保留您想要的方向

夫特

import UIKit

class ViewController: UIViewController,UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!
    var termsWeb = false
    var validOrientation : UIInterfaceOrientationMask = UIInterfaceOrientationMask.Portrait
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.loadWebView()
    }

    @IBAction func loadAnotherWeb(sender: AnyObject) {
        self.loadWebView()
    }

    func loadWebView()
    {
        let termsFilePath = NSBundle.mainBundle().pathForResource("terms_conditions", ofType: "html")
        do
        {
            var URLname = ""
            if(termsWeb)
            {
                URLname = "terms"
                termsWeb = !termsWeb
            }else
            {
                URLname = "another"
                termsWeb = !termsWeb
            }

            let html = try String(contentsOfFile: termsFilePath!)
            webView.loadData(html.dataUsingEncoding(NSUTF8StringEncoding)!, MIMEType: "text/html", textEncodingName: "utf-8", baseURL: NSURL(string: URLname)!)
        }catch
        {

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    internal func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
    {

        //url name another will show in landscape mode
        if(request.URL?.absoluteString == "file://terms")
        {
            print(request.description)

            if(UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
            {
                let value = UIInterfaceOrientation.LandscapeRight.rawValue
                self.validOrientation = .Landscape
                UIDevice.currentDevice().setValue(value, forKey: "orientation")
            }
        }

        //url name another will show in all modes
        if(request.URL?.absoluteString == "file://another")
        {
           self.validOrientation = .All
        }

        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return self.validOrientation
    }

}

我希望这有助于你

相关问题