iOS:如何将css文件注入到从Swift中的外部URL加载的webview链接

时间:2015-07-16 09:13:48

标签: ios swift uiwebview

我正在尝试将自定义css文件添加到uiwebview以将一些元素放入display:none,但经过大量的Google搜索后,我无法找到方法。我似乎应该通过javascript注入它,但我真的不知道如何。以前有人这么做过吗?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,当您加载html文件时,您可以在项目中添加CSS文件并在webview中加载html,请参阅: - link

import UIKit


class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let localfilePath = NSBundle.mainBundle().URLForResource("test", withExtension: "html");
        let myRequest = NSURLRequest(URL: localfilePath!);
        webView.loadRequest(myRequest);
        //or use as below 
        let myHTMLString:String! = "<h1>Hello Pizza!</h1>"
        webView.loadHTMLString(myHTMLString, baseURL: nil)
    }

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



}