WKWebView fullscreen instead of frame?

I am new to Swift 3.0 and I need help ...

I am trying to create a website display frame. The website does not have to fill in the whole view, but only the frame. Every time I launch the application, the website fills the entire screen, not just the frame ... :(

This is my code:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {


    @IBOutlet weak var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let myURL = URL(string: "https://www.google.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

What I want:

image

What I get:

image

+4
source share
1 answer

Add constraints (bindings) for peer-to-peer web browsing.

Try this with the layout:

Remove the code loadView()from your file:

enter image description here

Or try this programmatically:

import UIKit
import WebKit

class WebKitController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let myURL = URL(string: "https://www.google.com/")
        let myRequest = URLRequest(url: myURL!)
        setupWKWebViewConstraints()
        webView.load(myRequest)
    }

    // add constraints to your web view
    func setupWKWebViewConstraints() {

        let paddingConstant:CGFloat = 30.0

        webView.translatesAutoresizingMaskIntoConstraints = false

        webView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: paddingConstant).isActive = true
        webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -paddingConstant).isActive = true
        webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: paddingConstant).isActive = true
        webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -paddingConstant).isActive = true
    }

}
+1
source

Source: https://habr.com/ru/post/1687716/


All Articles