3

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
Birger
  • 349
  • 1
  • 4
  • 18

6 Answers6

2

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)
    }
}
Dixit Akabari
  • 2,419
  • 13
  • 26
0

I ran your code nothing is wrong in it.

Except the frame

The change you require -

  let myWebView:UIWebView = UIWebView(frame: self.view.frame)

Check your internet connection if you are not able to load from url

webView:didFailLoadWithError

implement this function to check any future errors you get

0

It's happen Because you haven't added myWebView to view.

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

    self.view.addSubview(myWebView)
    let request = URLRequest(url: URL(string: "https://google.com")!)
    myWebView.loadRequest(request)
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
0
Hear is your answers of two questions

**1.How do I get the web view to get fullscreen size?**

**Ans**: let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))

**2 Why can't I load a url - I only get the white screen**
**Ans**:
        let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        let url = URL (string: "https://google.com");
        let request = URLRequest(url: url! as URL);
        myWebView.loadRequest(request)
        self.view.addSubview(myWebView)

Load request before adding to self.view like above code
0

Change your webview size acooridng to your content after finishing your webview loading

 //MARK: - WebView Delegate Method
func webViewDidFinishLoad(_ webView: UIWebView) {
    var frame = self.webView.frame
    frame.size.height = 1
    self.webView.frame = frame
    let fittingSize = self.webView.sizeThatFits(CGSize.zero)
    frame.size = fittingSize
    self.webView.needsUpdateConstraints()
    self.webView.frame.size.height = fittingSize.height
    self.webView.scrollView.isScrollEnabled = false
}
Nishant Bhindi
  • 2,242
  • 8
  • 21
-1

You have initilized myWebView with fixed height and width i.e 300.Try

let frame = self.view.frame
let width = frame.width
let height = frame.height
let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: width, height: height))

Using storyboard you can drag and drop webView in viewController and give constraints to your view (top: 0, right: 0, bottom: 0, left: 0) so that your web view will appear full screen. Now extend outlet of webView to the viewController and in viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view
    let url = URL (string: "https://www.simplifiedios.net")
    let request = URLRequest(url: URL: url!)
    webView.loadRequest(request)
}
laxman khanal
  • 998
  • 1
  • 8
  • 18
  • is not a answer related to the ?, use myWebView.frame = self.view.bounds and als check transport security is added or not in your .plist – Anbu.Karthik Sep 20 '17 at 12:03