Search code examples
iosswiftuiwebview

IOS webview in full screen


This is properly a very simple question, but I'm completly new to IOS dev and Swift.

I'm trying to add a webview and make it fill the entire screen.

This is my code

 override func viewDidLoad() {
        super.viewDidLoad()

        let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

        self.view.addSubview(myWebView)
        let url = URL (string: "https://google.com");
        let request = URLRequest(url: url! as URL);
        myWebView.loadRequest(request);
        }

When I run the app only a white screen appear. I can load html as a string (loadHTMLString)

So 2 questions

  • How do I get the web view to get fullscreen size?
  • Why can't I load a url - I only get the white screen

Solution

  • Try This.

    class ViewController: UIViewController , UIWebViewDelegate {
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
    
            let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height))
            myWebView.delegate = self
            self.view.addSubview(myWebView)
            let url = URL (string: "https://google.com");
            let request = URLRequest(url: url! as URL);
            myWebView.loadRequest(request);
    
    
        }
    
        func webViewDidStartLoad(_ webView: UIWebView) {
            print("web view start loading")
        }
    
        func webViewDidFinishLoad(_ webView: UIWebView) {
            print("web view load completely")
        }
    
        func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
            print("web view loading fail : ",error.localizedDescription)
        }
    }