【发布时间】:2022-01-03 20:44:12
【问题描述】:
我想弄清楚如何使用 UINavigationController。每当我创建一个,并将 UIViewController 设置为 rootViewController 时,它将 UIViewController 的背景颜色显示为状态栏的颜色,如下所示:
UIViewController .backgroundColor 设置为 System Teal Color,Navigation Bar .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
}
}
这是本示例的项目。 Example Project
【问题讨论】:
-
好的,我试过这个方法,现在导航栏很短,像这样:drive.google.com/file/d/1-sv9AOFeciDveHRyIFpaKAg-CcijM26V/…
-
哦,废话,我在这个项目“TabbarColor”中使用了一种不存在的颜色。我将其更改为 UIColor.gray,它现在可以正常工作了。谢谢!
标签: ios swift iphone ipad uinavigationcontroller