[Xcode10 实际操作]三、视图控制器-(6)UINavigationController导航栏样式

时间:2022-11-28 20:44:16

本文将演示对导航栏进行样式设置,以及更改导航顶部的提示区。

选择编辑第一个视图控制器文件。

 1 import UIKit
 2 
 3 class FirstSubViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7 
 8         // Do any additional setup after loading the view.
 9         self.title = "First Page"
10         self.view.backgroundColor = UIColor.brown
11         self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: UIBarButtonItem.Style.plain, target: self, action: #selector(FirstSubViewController.nextPage))
12     }
13     
14     @objc func nextPage()
15     {
16         let viewController = SecondSubViewController()
17         self.navigationController?.pushViewController(viewController, animated: true)
18     }
19 
20     //建立视图控制器对象的生命周期中的,视图即将显示的代理方法。
21     //视图在即将显示时,,执行这个方法
22     override func viewWillAppear(_ animated: Bool) {
23         //首先实现父类的同名方法
24         super.viewWillAppear(animated)
25         //设置顶部导航区的提示文字
26         self.navigationItem.prompt = "Loading..."
27         //设置导航栏的背景是否透明
28         self.navigationController?.navigationBar.isTranslucent = false
29         //设置导航栏的系统样式
30         self.navigationController?.navigationBar.barStyle = UIBarStyle.black
31         //设置导航栏的前景颜色。
32         self.navigationController?.navigationBar.tintColor = UIColor.orange
33     }
34 
35     override func didReceiveMemoryWarning() {
36         super.didReceiveMemoryWarning()
37         // Dispose of any resources that can be recreated.
38     }
39 }