我按照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.
}
答案 0 :(得分:2)
这是最终为我工作的东西(非常类似于Herbert Bay的回答)。
<强>夫特:强>
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 设置为状态栏的高度,可以消除Web视图和状态栏的任何重叠。
答案 1 :(得分:1)
我会将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];
答案 2 :(得分:0)
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
}
答案 3 :(得分:-1)
我通过在主“根”视图中嵌入容器视图解决了这个问题,然后在容器内放置了一个webView,从而能够通过设置约束来控制容器显示在状态栏下
Container View.top =顶部布局Guide.bottom
我必须说我很惊讶于这么多在线教程可以从一些抛光/升级中受益,“读者要小心”,这在你学习时很难叹息。