简体   繁体   中英

Swift Webview force landscape

I create a webview in xCode with Swift. Now I want some pages that the screen force rotates to landscape. And some pages auto orientation. How can I do this?

UPDATE

I did this after the answer of 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)

But it doesn't work. When I turn my device to portrait, the app rotate to portrait but I want to force landscape.

I had been working on your question, and this are my results, first of all you need to be notified when the event of load new URL happens with this method you can achieve this, of course you need to set as delegate of your webView your viewController and then implement this method

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

this is the code

UPDATED

to keep this orientation you need to implement this method func supportedInterfaceOrientations() -> UIInterfaceOrientationMask and keep in one variable the orientation that you want


Swift

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
    }

}

I hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM