WKWebView Add to SubView

后端 未结 1 1903
青春惊慌失措
青春惊慌失措 2021-01-02 05:20

I can add a WKWebView programmatically to a subview with the code below. How can I add WKWebView to the view containerView2 that was added via Interface Builder?

<         


        
相关标签:
1条回答
  • 2021-01-02 06:04

    If containerView2 is created via Interface Builder and containerView is basically your WKWebView loadView() method might look like:

        override func loadView() {
            super.loadView()
    
            webView = WKWebView()
            containerView = self.webView!
    
            containerView.frame = containerView2.frame
            containerView2.addSubview(containerView)
        }
    

    And of course you can have better names for your views. For example, the "parent" element could be called containerView and your WKWebView could just be called webView. In this way code becomes much more readable:

        override func loadView() {
            super.loadView()
    
            webView = WKWebView()
    
            if (webView != nil) {
                webView!.frame = containerView.frame
                containerView.addSubview(webView!)
            }
        }
    
    0 讨论(0)
提交回复
热议问题