How to create WKWebView size in Swift 3 / iOS 10

I am trying to add WKWebView to my application, which occupies about ~ 2/3 of the screen (leaving a place below). Since there seems to be no way to add WKWebViewto the storyboard, I added the usual one UIView. Then in my method viewDidAppearI try to create WKWebViewwith the same borders. Is this a reasonable approach?

My code is as follows:

    @IBOutlet var conversationView: UIView!

    override func viewDidAppear(_ animated: Bool) {
      let webView = WKWebView(frame: conversationView.frame)
      if let url = URL(string: "https://www.google.com") {
        webView.load(URLRequest(url: url))
      }

      // conversationView = webView // <-- doesn't work :\
      conversationView.addSubview(webView) // works!
    }

Changing views does not directly work at all, so instead I add a web view as a subview of the temporary view that I added to the storyboard. This at least allows me to limit his position and size, but still he feels very stupid and as if there should be a better way to do this.

Screenshot

+4
5

. Samip Shah , . , translatesAutoresizingMaskIntoConstraints false.

, . UIView , WKWebView. :

@IBOutlet var contentView: UIView!
var wkWebView:WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    wkWebView = WKWebView(frame:contentView.frame)
    contentView.addSubview(wkWebView)
    constrainView(view: wkWebView, toView: contentView)

    let request = URLRequest(url: /* your url here */)
    wkWebView.load(request)
}

. , .

func constrainView(view:UIView, toView contentView:UIView) {
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
+6

; "-", - . - , .

+1

- , .

     @IBOutlet weak var wkWebBackgroundView: UIView!
     var wkWebView:WKWebView!

     override func viewDidLoad() {
            super.viewDidLoad()
            setUpView()

        }
     func setUpView(){
            self.wkWebView = WKWebView(frame: CGRect.zero)     
            self.wkWebBackgroundView.addSubview(wkWebView)

            let height = NSLayoutConstraint(item: wkWebView, attribute: .height, relatedBy: .equal, toItem: wkWebBackgroundView, attribute: .height, multiplier: 1, constant: 0)
            let width = NSLayoutConstraint(item: wkWebView, attribute: .width, relatedBy: .equal, toItem: wkWebBackgroundView, attribute: .width, multiplier: 1, constant: 0)
            let top = NSLayoutConstraint(item: wkWebView, attribute: .top, relatedBy: .equal, toItem: wkWebBackgroundView, attribute: .top, multiplier: 1, constant: 0)
            let leading = NSLayoutConstraint(item: wkWebView, attribute: .leading, relatedBy: .equal, toItem: wkWebBackgroundView, attribute: .leading, multiplier: 1, constant: 0)

            view.addConstraints([leading, top, height, width])  
        }
+1

- .

let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height * 0.66).integral)
view.addSubview(webView)

, , subview, .

: , , :

let webView = WKWebView(frame: CGRect(x: label.frame.maxX + (whatever distance you want between label bottom and webView top), y: 0, 
width: view.bounds.width, height: view.bounds.height * 0.66).integral)
view.addSubview(webView)
0

,

. , Frame .

import UIKit
import WebKit

class WKWebViewVC: UIViewController {

    @IBOutlet weak var wkWebviewBGView: UIView!
    var wkWebview: WKWebView!

    override func viewDidLoad() {

       super.viewDidLoad()

       wkWebview = WKWebView(frame: wkWebviewBGView.bounds, configuration: WKWebViewConfiguration())
       wkWebview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
       self.wkWebviewBGView.addSubview(wkWebview)

       let url = URL(string: "https://www.google.com")
       wkWebview.load(URLRequest(url: url!))
   }
}
0

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


All Articles