Swift 3 WebView

前端 未结 5 1820
走了就别回头了
走了就别回头了 2021-02-04 00:02

So I just updated to the new Xcode8 and Swift3 but now my web view does not work. Here is the code I used:

UIWebView.loadRequest(webVie         


        
5条回答
  •  梦毁少年i
    2021-02-04 00:39

    A dynamic webview class with loading :

    class WebViewController : UIViewController, UIWebViewDelegate {
    
    var url : String!
    var webView : UIWebView!
    var loadingView : UIActivityIndicatorView!
    
    override func viewDidLoad() {
    
    
        let x = Int(UIScreen.main.bounds.width / 2 - 25)
        loadingView = UIActivityIndicatorView(frame : CGRect(x: x, y: 100, width: 50, height: 50))
        loadingView.activityIndicatorViewStyle = .gray
    
        webView = UIWebView(frame: UIScreen.main.bounds)
        webView.delegate = self
        view.addSubview(webView)
        if let url = URL(string: url) {
            let request = URLRequest(url: url)
            webView.loadRequest(request)
        }else{
    
            self.navigationController?.popViewController(animated: true)
        }
    
        view.addSubview(loadingView)
        loadingView.startAnimating()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
    
        loadingView.removeFromSuperview()
    }
    
    static func initController() -> WebViewController {
    
        let vc = WebViewController()
        return vc
    }   
    }
    

    ///// use it

     let webvc = WebViewController.initController()
     webvc.url = "http://apple.com"
     self.navigationController?.pushViewController(webvc, animated: true)
    

提交回复
热议问题