我使用 UINavigationController
作为 UIViewController
的根 Controller 。在 UIViewController 中,我将自定义 UIView
添加到导航栏中(因为我想将 CAGradientLayer
使用到我的自定义 UIView
)。
一切工作正常,除了我的自定义 UIView
不与状态栏重叠,这意味着我的自定义 UIView
从导航栏顶部填充。
有没有什么解决方案和方法可以让它与statusBar重叠?在这里,我包含了我想要实现的 UI。
最佳答案
状态栏总是在那里,所以如果您在添加 View 时不想要它,则必须重写添加新 View 的 ViewController 的属性 prefersStatusBarHidden
,如下所示:-
声明一个新变量:
var myNewViewIsVisible: Bool = false {
didSet {
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
然后像这样覆盖该属性: -
override var prefersStatusBarHidden: Bool {
return myNewViewIsVisible
}
解释: 首先声明变量并观察它的变化。如果它被更改,即..当您添加新 View 时,将其更改为true,然后强制ViewController更新statatusBar,这将导致它隐藏,因为您的myNewViewIsVisible将为true。
关于ios - 在 UINavigationBar 顶部添加 UIView 时如何重叠状态栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567664/