Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios WKwebview内容覆盖顶部状态栏_Ios_Swift_Wkwebview - Fatal编程技术网

Ios WKwebview内容覆盖顶部状态栏

Ios WKwebview内容覆盖顶部状态栏,ios,swift,wkwebview,Ios,Swift,Wkwebview,我按照教程创建了一个WKwebview,但有一个问题。顶部的状态栏由webView的内容覆盖 既然webView是containerView,那么webView如何在代码中处于状态栏之下呢。谢谢 import UIKit import WebKit class ViewController: UIViewController { @IBOutlet var mainView : UIView? var webView: WKWebView? override func loadView()

我按照教程创建了一个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.
}

}

我通过在主“根”视图中嵌入一个容器视图来解决这个问题,然后在容器中放置一个webView,这样就可以通过设置其约束来控制容器显示在状态栏下

Container View.top=顶部布局指南.bottom


我必须说,我对如此多的在线教程感到惊讶,这些教程可以从一些改进/升级中获益,“读者当心”,这在你学习时是很困难的,叹气。

我将添加
WKWebView
作为容器视图的子视图,并使用状态栏的大小以
initWithFrame
初始化
WKWebView
。很高兴知道是否有更好的方法

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];

以下是最终对我有用的东西(与赫伯特·贝的答案非常相似)

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设置为状态栏的高度可以消除web视图和状态栏的重叠。

能否显示最终使用的代码?我特别感兴趣的是您如何添加容器和
WKWebView
。您是否使用故事板添加容器?提前谢谢@Dev iL我离开了这个项目,我的xCode/swift上已经生锈了。我知道lynda.com课程对我帮助很大。对不起,没什么帮助。
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
    }