WKwebview内容覆盖顶部状态栏

WKwebview content covers the top status bar

我按照 this 教程创建了一个可以工作但有一个问题的 WKwebview。顶部状态栏被 webView 的内容覆盖。

既然webView是containerView,那webView在代码中怎么去到状态栏下面呢。谢谢

import UIKit
import WebKit

class ViewController: UIViewController {
@IBOutlet var mainView : UIView?
var webView: WKWebView?

override func loadView() {
    super.loadView() // call parent loadView
    self.webView = WKWebView() // instantiate WKWebView
    self.view = self.webView! // make it the main view
}

override func viewDidLoad() {
    super.viewDidLoad() // run base class viewDidLoad
    let url = NSURL(string:"https://www.google.com") // make a URL
    let req = NSURLRequest(URL:url!) // make a request w/ that URL
    self.webView!.loadRequest(req) // unwrap the webView and load the request.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

我通过在 "root" 主视图中嵌入一个容器视图解决了这个问题,然后在容器中放置了一个 webView,因此能够通过设置其约束来控制容器显示在状态栏下方

Container View.top = Top Layout Guide.bottom

我必须说我很惊讶有这么多在线教程可以从一些polishing-up/upgrade中受益,"reader beware"这在你学习的时候很难,叹气。

我会添加 WKWebView 作为容器视图的子视图,并使用状态栏的大小将 WKWebView 初始化为 initWithFrame。如果有更好的方法,很高兴学习。

WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
CGFloat heightStatusBar = [UIApplication sharedApplication].statusBarFrame.size.height;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0.0, heightStatusBar,
                                                           self.view.frame.size.width,
                                                           self.view.frame.size.height-heightStatusBar)
                                  configuration:webViewConfiguration];
[self.view addSubview:_webView];

这就是最终对我有用的东西(与 Herbert Bay 的回答非常相似)。

Swift:

class ViewController: UIViewController, WKNavigationDelegate {

    let webView = WKWebView()

    override func viewDidLoad() {
        // Get height of status bar (iPhone X introduced a new status bar)
        let statusBarHeight = UIApplication.shared.statusBarFrame.height
        // Initialize the frame
        webView.frame = CGRect.init(x: 0, y: statusBarHeight, width: view.bounds.maxX, height: view.bounds.maxY)
        // Set background color of status bar (optional)
        self.view.backgroundColor = UIColor(red: 248/255.0, green: 248/255.0, blue: 248/255.0, alpha: 1.0)
    }
}

CGRect.init() 中将 y 设置为状态栏的高度可以消除您的网络视图的任何重叠,并且状态栏。

webView.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
        webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    } else {
        // Fallback on earlier versions
        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    }
    webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    if #available(iOS 11.0, *) {
        webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    } else {
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }