【发布时间】:2019-05-28 21:53:16
【问题描述】:
在状态栏添加渐变层并为不同的视图控制器使其变亮或变暗后,在导航中的某个随机点,我在缺口 iPhone 状态栏中的时钟已被截断。像 1... 或 ...
我已经尝试了这两种解决方案来使状态栏内容变白;但这对这种随机时钟行为没有影响。
这个:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barStyle = .blackOpaque
self.setNeedsStatusBarAppearanceUpdate()
}
或者这个:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
它们都改变了我的状态栏内容颜色。关于导致这种行为的原因有什么想法吗???
这就是我使状态栏渐变的方式:
extension UIViewController {
func makeStatusBarGradient(){
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let gradientLayer1 = CAGradientLayer()
gradientLayer1.frame = statusBarView.frame
gradientLayer1.colors = [UIColor.APColors.redGradient.cgColor, UIColor.APColors.orangeGradient.cgColor]
gradientLayer1.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer1.endPoint = CGPoint(x: 1.0, y: 0.5)
gradientLayer1.cornerRadius = 0
gradientLayer1.zPosition = -10
gradientLayer1.name = "gradient"
//Change status bar color
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView{
statusBar.layer.addSublayer(gradientLayer1)
}
setNeedsStatusBarAppearanceUpdate()
}
func clearStatusBarGradient(){
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView{
if let layers = statusBar.layer.sublayers?.filter({$0.name == "gradient"}){
if (layers.count > 0){
layers.first?.removeFromSuperlayer()
}
}
}
setNeedsStatusBarAppearanceUpdate()
}
}
而且,APColors 是:
extension UIColor {
struct APColors {
static var redGradient : UIColor { return UIColor(red: 1.00, green: 0.42, blue: 0.24, alpha: 1) }
static var orangeGradient : UIColor { return UIColor(red: 0.95, green: 0.19, blue: 0.42, alpha: 1)}
}
}
【问题讨论】:
-
您能分享一下如何将渐变颜色应用于状态 vbar 吗?
-
@EmreÖnder 已更新。
标签: ios swift statusbar clock iphone-x