我有一个非常简单的Web视图项目,该项目仅加载一个网站。但是,存在一个错误,如果我向下滚动,网站内容会显示在状态栏中。
我读到这是因为ViewController的背景色设置为透明,如何将其更改为另一种颜色?
我这样尝试过:
let color = UIColor.black;
self.view.backgroundColor = color
但是什么都没有改变
整个代码:
ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// 1 The webView loads the url using an URLRequest object.
let url = URL(string: "https://www.blizz-z.de/")!
webView.load(URLRequest(url: url))
// 2 A refresh item is added to the toolbar which will refresh the current webpage.
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let color = UIColor.black;
self.view.backgroundColor = color
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
我可以使用以下代码禁用状态栏以修复该错误,但我尝试保留它:
override var prefersStatusBarHidden: Bool {
return true
}
答案 0 :(得分:1)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let height = UIScreen.main.bounds.height
let width = UIScreen.main.bounds.width
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: width, height: height))
webView.load(URLRequest(url: (URL(string: "https://www.blizz-z.de/")!)))
view.addSubview(webView)
UIApplication.shared.statusBarView?.backgroundColor = #colorLiteral(red: 0.9529411765, green: 0.9529411765, blue: 0.9529411765, alpha: 1)
}
}
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector("statusBar")) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
答案 1 :(得分:-1)
使safeArea.top
约束相对于superview.top
约束,而不是webView
约束。这样,您就可以在状态栏下创建="This is a date formatted manually: "&TEXT('Master sheet'!B3,"hhmm")&
"hrs of the following date: "&TEXT('Master sheet'!B3, "dd/mm/yyyy")&" AND THIS IS IT"
。