提问人:Build 提问时间:1/4/2022 最后编辑:Build 更新时间:1/4/2022 访问量:1268
UINavigationController 状态栏颜色为透明 [重复]
UINavigationController status bar color is transparent [duplicate]
问:
我正在尝试弄清楚如何使用UINavigationController。每当我创建一个UIViewController并将UIViewController设置为rootViewController时,它都会将UIViewController的背景色显示为状态栏的颜色,如下所示:
UIViewController .backgroundColor 设置为 System Teal Color,导航栏 .background 颜色为 UIColor.gray。因此,根视图控制器通过状态栏区域中的导航栏显示。如何使状态栏与导航栏的颜色相同?为什么那个区域是透明的?
下面是主视图控制器类,其中包含 UINavigationController 作为子类。
class ViewController: UIViewController {
let navVC: UINavigationController?
var mainVC: UIViewController?
required init?(coder: NSCoder) {
mainVC = MainVC(nibName: "MainVC", bundle: nil)
mainVC?.title = "Main"
navVC = UINavigationController(rootViewController: mainVC!)
navVC?.navigationBar.backgroundColor = UIColor.gray
super.init(coder: coder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navVC?.willMove(toParent: self)
self.addChild(navVC!)
self.view.addSubview(navVC!.view)
navVC?.didMove(toParent: self)
navVC?.view.translatesAutoresizingMaskIntoConstraints = false
navVC?.view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
navVC?.view.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
navVC?.view.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
navVC?.view.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
}
}
下面是此示例的项目。示例项目
答:
1赞
Build
1/4/2022
#1
感谢 lazarevzubov,他提出了这个答案。设置 navigationBar.standardAppearance 和 navigationBar.scrollEdgeAppearance 解决了该问题。
评论