【问题标题】:WKWebView - Margin TopWKWebView - 边距顶部
【发布时间】:2021-03-23 14:12:32
【问题描述】:

我马上开始了。

代码是一个没有高级导航的webview,约束从顶部移动webview,留下一个上边距。

看图

Image Result

我的代码

import UIKit 
import WebKit 

class ViewController: UIViewController,WKNavigationDelegate, WKUIDelegate {

lazy var webView: WKWebView = {
    let webConfiguration = WKWebViewConfiguration()
    let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.navigationDelegate = self
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

func setupUI() {
    self.view.backgroundColor = .red
    self.view.addSubview(webView)
    NSLayoutConstraint.activate([
        webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
        webView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
        webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
        webView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor)
    ])
}

override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
    
        let myURL = URL(string: "https://www.google.com.br/")
        let myRequest = URLRequest(url: myURL!,cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy,timeoutInterval: 10.0)
        webView.load(myRequest)
    
}

}

【问题讨论】:

  • 尝试从webView顶部移除安全区域约束,将其更改为self.view.topAnchor
  • 我试过修改。 NSLayoutConstraint.activate ([ webView.topAnchor.constraint (equalTo: self.view.topAnchor), webView.leftAnchor.constraint (equalTo: self.view.leftAnchor), webView.bottomAnchor.constraint (equalTo: self.view.bottomAnchor), webView.rightAnchor.constraint (equalTo: self.view.rightAnchor) ]) 边距可见。
  • 您可以添加自己的答案,然后将问题标记为solved,这样其他人就会看到这个问题的答案并知道有解决方案。 stackoverflow.com/help/self-answer

标签: swift wkwebview


【解决方案1】:

解决了。只需添加:

self.navigationController? .isNavigationBarHidden = true

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2018-08-07
    相关资源
    最近更新 更多