如何设置背景色为透明,以显示运营商信息、时间和电池状态
how to set background colour to transparent, so as to show carrier information, time and battery status
我正在使用 WKWebView 在我的 Swift 3+ 应用程序中打开网页。
我没有使用故事板或 XIB。
当我的 webview 加载时,由于黑色背景颜色(默认设置),它隐藏了运营商信息、蜂窝网络信号强度指示器(点)、WiFi/4G 连接指示器、时间和电池状态
我想在顶部状态栏下方显示我的网络视图。
我使用 UIApplication.shared.statusBarFrame.height
获得了状态栏的高度
(预计为 20,并已确认)
我怎样才能让两者看起来都很好? (带白色background/transparency)
这样用户就可以在顶部看到状态栏信息,然后按预期看到 webview。
Controller.swift:
import UIKit
import WebKit
class MyWebViewController: UIViewController, WKNavigationDelegate {
var urlString:String? = nil
let myWebView = WKWebView(frame:CGRect(x: 0, y: 32+UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-32-UIApplication.shared.statusBarFrame.height))
let button = UIButton(frame: CGRect(x:UIScreen.main.bounds.width - 100,y:50,width:50,height:50))
let navpath = UITextView(frame:CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width, height: 32))
override func viewDidLoad() {
super.viewDidLoad()
self.view.isOpaque=true
self.view.backgroundColor = UIColor.clear
print("status bar height : \(UIApplication.shared.statusBarFrame.height)")
button.backgroundColor = UIColor.red
button.setTitle("close", for: .normal)
myWebView.navigationDelegate = self
myWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
myWebView.load(NSURLRequest(url: targetUrl as URL) as URLRequest)
view.addSubview(myWebView)
button.autoresizingMask = [.flexibleWidth, .flexibleHeight]
navpath.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(navpath)
myWebView.addSubview(button)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
//print(navigationAction.navigationType)
let navReq = navigationAction.request
let targetUrl = navReq?.url
let link = targetUrl?.absoluteString
print("targetURL : \(targetUrl)\n link : \(link)")
navpath.text=link
//self.myWebView.load(navReq)
decisionHandler(.allow)
}
}
旋转时,绿色电池指示灯隐藏。我也想解决这个问题。导航路径(地址栏文本持有者)和按钮(关闭)也放错了地方,我想我需要 autolayout/constraintmanager 或其他东西。
当前样本加载duckduckgo:
纵向
横向
此外,请告诉我 UITextView 是否是显示地址栏(网页的 URL)的不错选择,或者我应该使用 UILabel 或其他可能更好的导航栏?但是,我想这需要 storyboard/xib.
您可以为您的视图设置 prefferedStatusBarStyle
:
https://developer.apple.com/reference/uikit/uiviewcontroller/1621416-preferredstatusbarstyle
在你的 ViewController:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
如果不同的视图有不同的状态栏样式,您可以使用setNeedsStatusBarAppearanceUpdate()
更新它,例如。在 viewDidAppear
.
我正在使用 WKWebView 在我的 Swift 3+ 应用程序中打开网页。 我没有使用故事板或 XIB。
当我的 webview 加载时,由于黑色背景颜色(默认设置),它隐藏了运营商信息、蜂窝网络信号强度指示器(点)、WiFi/4G 连接指示器、时间和电池状态
我想在顶部状态栏下方显示我的网络视图。
我使用 UIApplication.shared.statusBarFrame.height
获得了状态栏的高度
(预计为 20,并已确认)
我怎样才能让两者看起来都很好? (带白色background/transparency) 这样用户就可以在顶部看到状态栏信息,然后按预期看到 webview。
Controller.swift:
import UIKit
import WebKit
class MyWebViewController: UIViewController, WKNavigationDelegate {
var urlString:String? = nil
let myWebView = WKWebView(frame:CGRect(x: 0, y: 32+UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-32-UIApplication.shared.statusBarFrame.height))
let button = UIButton(frame: CGRect(x:UIScreen.main.bounds.width - 100,y:50,width:50,height:50))
let navpath = UITextView(frame:CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width, height: 32))
override func viewDidLoad() {
super.viewDidLoad()
self.view.isOpaque=true
self.view.backgroundColor = UIColor.clear
print("status bar height : \(UIApplication.shared.statusBarFrame.height)")
button.backgroundColor = UIColor.red
button.setTitle("close", for: .normal)
myWebView.navigationDelegate = self
myWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
myWebView.load(NSURLRequest(url: targetUrl as URL) as URLRequest)
view.addSubview(myWebView)
button.autoresizingMask = [.flexibleWidth, .flexibleHeight]
navpath.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(navpath)
myWebView.addSubview(button)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
//print(navigationAction.navigationType)
let navReq = navigationAction.request
let targetUrl = navReq?.url
let link = targetUrl?.absoluteString
print("targetURL : \(targetUrl)\n link : \(link)")
navpath.text=link
//self.myWebView.load(navReq)
decisionHandler(.allow)
}
}
旋转时,绿色电池指示灯隐藏。我也想解决这个问题。导航路径(地址栏文本持有者)和按钮(关闭)也放错了地方,我想我需要 autolayout/constraintmanager 或其他东西。
当前样本加载duckduckgo:
纵向
横向
此外,请告诉我 UITextView 是否是显示地址栏(网页的 URL)的不错选择,或者我应该使用 UILabel 或其他可能更好的导航栏?但是,我想这需要 storyboard/xib.
您可以为您的视图设置 prefferedStatusBarStyle
:
https://developer.apple.com/reference/uikit/uiviewcontroller/1621416-preferredstatusbarstyle
在你的 ViewController:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
如果不同的视图有不同的状态栏样式,您可以使用setNeedsStatusBarAppearanceUpdate()
更新它,例如。在 viewDidAppear
.