ios - Swift Webview 力风景

标签 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)

但它不起作用。当我将设备转为纵向时,应用会旋转为纵向,但我想强制横向。

最佳答案

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

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

这是代码

已更新

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


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
    }

}

希望对你有帮助

关于ios - Swift Webview 力风景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631969/

相关文章:

ios - 如何以编程方式在 Today 扩展中设置 Root View Controller

objective-c - 带有&符号的文本文件被绘制到电子邮件消息停止模板中

SDK 'Unit Test Bundle' 中的产品类型 'iOS 9.2' 需要 iOS 代码签名

iOS9 - 分享到 Instagram(w/hooks)不工作

ios - iOS 的 FCM : invalid parameter, token 原因

ios - 无法在 Swift 3 中启动 Activity Indicator?

ios - 强制向下转型的安全方式

c# - 移动跨平台游戏开发的最佳编程语言

ios - 通过为 Swift 中的每个字符提供两种可能性来比较字符串

objective-c - MFMessageComposeViewController 的替代品