[Xcode 实际操作]三、视图控制器-(4)使用UINavigationController导航栏和工具栏
本文将演示如何显示和隐藏导航视图的导航栏和工具栏
打开第一个视图控制器
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 let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 14 imgView.image = UIImage(named: "PicRound") 15 let view = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 16 view.backgroundColor = UIColor.purple 17 view.addSubview(imgView) 18 self.navigationItem.titleView = view 19 } 20 21 @objc func nextPage() 22 { 23 let viewController = SecondSubViewController() 24 self.navigationController?.pushViewController(viewController, animated: true) 25 } 26 27 //首先建立视图控制器声明周期中,视图即将显示的代理方法 28 //视图在即将可见时,执行这个方法 29 override func viewWillAppear(_ animated: Bool) { 30 //先覆盖父类的同名方法 31 super.viewWillAppear(animated) 32 //显示导航视图控制器的顶部工具栏 33 self.navigationController?.setToolbarHidden(false, animated: false) 34 } 35 36 override func didReceiveMemoryWarning() { 37 super.didReceiveMemoryWarning() 38 // Dispose of any resources that can be recreated. 39 } 40 }
接着编辑第二个视图控制器
在第二个视图控制器中添加两个按钮元素
1 import UIKit 2 3 class SecondSubViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 self.title = "Second Page" 10 self.view.backgroundColor = UIColor.purple 11 12 //添加一个按钮,当单击这个按钮时,会隐藏导航栏。 13 let btHideNavBar = UIButton(frame: CGRect(x: 40, y: 200, width: 240, height: 30)) 14 //设置按钮上的标题文字 15 btHideNavBar.setTitle("Hide navigation bar", for: UIControl.State()) 16 //设置按钮的背景颜色为橙色 17 btHideNavBar.backgroundColor = UIColor.orange 18 //给当前按钮绑定点击事件 19 btHideNavBar.addTarget(self, action: #selector(SecondSubViewController.hideNavigatonBar), for: .touchUpInside) 20 //然后把按钮,插入到当前视图控制器的根视图 21 self.view.addSubview(btHideNavBar) 22 23 //使用同样的方法,创建另一个按钮 24 //点击这个按钮,会隐藏底部的工具栏 25 let btHideToolBar = UIButton(frame: CGRect(x: 40, y: 260, width: 240, height: 30)) 26 //设置按钮上的标题文字 27 btHideToolBar.setTitle("Hide tool bar", for: UIControl.State()) 28 //设置按钮的背景颜色为橙色 29 btHideToolBar.backgroundColor = UIColor.orange 30 //给当前按钮绑定点击事件 31 btHideToolBar.addTarget(self, action: #selector(SecondSubViewController.hideToolBar), for: .touchUpInside) 32 //然后把按钮,插入到当前视图控制器的根视图 33 self.view.addSubview(btHideToolBar) 34 } 35 36 //创建响应第一个按钮点击事件的方法 37 @objc func hideNavigatonBar() 38 { 39 //当用户点击第一个按钮时,隐藏导航视图控制器的顶部导航栏 40 self.navigationController?.setNavigationBarHidden(true, animated: true) 41 } 42 43 //接着创建响应第二个按钮的点击事件方法 44 @objc func hideToolBar() 45 { 46 //当用户点击第二个按钮时,隐藏导航视图控制器的顶部工具栏 47 self.navigationController?.setToolbarHidden(true, animated: true) 48 } 49 50 override func didReceiveMemoryWarning() { 51 super.didReceiveMemoryWarning() 52 // Dispose of any resources that can be recreated. 53 } 54 }