默认情况下,iPhone X上的状态栏如下所示:
但我想实现这个目标:
我尝试设置preferredStatusBarStyle为lightContent但只有在将状态栏后面的背景设置为黑色后才能使用.
为了修复圆角,我最后添加了另一个圆角的子视图.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let roundedView = UIView(
frame: CGRect(
x: 0,
y: UIApplication.shared.statusBarFrame.height,
width: view.frame.width,
height: view.frame.height
)
)
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
roundedView.backgroundColor = .white
view.addSubview(roundedView)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 60))
label.text = "Black statusbar!"
label.textAlignment = .center
roundedView.addSubview(label)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是最好的方法..必须有更好的方法来实现这一目标.
UPDATE
这是一个可怕的想法,因为: …
对于ios 13,我无法设置状态栏的文本颜色。如何获取statusBarManager的视图?如何仅更改文本颜色?
由于:
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“在UIApplication上名为-statusBar或-statusBarWindow的应用程序:此代码必须更改,因为不再有状态栏或状态栏窗口。而是在窗口场景上使用statusBarManager对象。”
我当前的代码:
func setStatusBarTextColor(_ color: UIColor) {
if #available(iOS 13.0, *) {
// How to do for iOS 13??
} else {
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
statusBar.setValue(color, forKey: "foregroundColor")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经找到了这个/sf/answers/4017632601/,但这不是我想要的