swift4--UINavigationController(顶部导航栏)

UINavigationController的视图控制器(顶部导航栏)

1.添加两个视图控制器文件

FirstViewContorller

//
//  ViewController0202.swift
//  演示demo


import UIKit

class ViewController0202: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //加载视图后进行任何其他设置。
        
        self.title = "第一页"
        
        self.view.backgroundColor = UIColor.white
        
        //设置右上角导航按钮的样式和功能
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Next",style:UIBarButtonItem.Style.plain,target:self,action:#selector(nextPage))
        
    }
    
    //按钮的点击事件
    
    @objc func nextPage(){
        
        //初始化第二个视图控制器对象
        
        let viewController = ViewController0202_1()
        
        //将第二个视图控制器压入导航视图控制器,实现页面跳转
        
        self.navigationController?.pushViewController(viewController,animated:true)
        
    }
    
        //视图即将可见时执行该方法
    //        导航栏的样式设置
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        设置是否透明
//        self.navigationController?.navigationBar.isTranslucent = false
//        设置系统样式
//        self.navigationController?.navigationBar.barStyle = .black
//        设置背景颜色
//        self.navigationController?.navigationBar.barTintColor = UIColor.orange
    }


}

SecondViewContorller

//
//  ViewController0202_1.swift
//  演示demo
//
//  Copyright © 2018 antu. All rights reserved.
//

import UIKit

class ViewController0202_1: UIViewController {

    var isHideNavBar:Bool = false
    var isHideToolBar:Bool = false
    
    override func viewDidLoad () {
        
        super.viewDidLoad()
        
        //加载视图后进行任何其他设置。
        
        self.title = "第二页"
        
        self.view.backgroundColor = UIColor.white
        
        let btHideNavBar = UIButton(frame: CGRect(x: 60, y: 200, width: 240, height: 50))
        // 设置按钮上的标题文字
        
        btHideNavBar.setTitle("隐藏或显示顶部导航栏", for: UIControl.State())
        
        btHideNavBar.backgroundColor = UIColor.orange
        
        btHideNavBar.addTarget(self, action: #selector(hidNavigationBar), for: UIControl.Event.touchUpInside)
        
        self.view.addSubview(btHideNavBar)
        
        let btHideToolBar = UIButton(frame: CGRect(x: 60, y: 300, width: 240, height: 50))
        
        btHideToolBar.setTitle("隐藏或显示底部工具栏", for: UIControl.State())
        
        btHideToolBar.backgroundColor = UIColor.orange
        
        btHideToolBar.addTarget(self, action: #selector(hideToolBar), for: UIControl.Event.touchUpInside)
        
        self.view.addSubview(btHideToolBar)
        
    }
    
    // 隐藏或显示顶部导航栏的方法
    
    @objc func hidNavigationBar (){
        if isHideNavBar == false {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
            isHideNavBar = true
        } else {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            isHideNavBar = false
        }
        
    }
    
    // 隐藏或显示底部工具栏的方法
    
    @objc func hideToolBar () {
        
        if isHideToolBar == false {
            self.navigationController?.setToolbarHidden(true, animated: true)
            isHideToolBar = true
        } else {
            self.navigationController?.setToolbarHidden(false, animated: true)
            isHideToolBar = false
        }
    }

}

AppDelegate中

func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {

//在应用程序启动后覆盖自定义点。

让viewContorller = FirstSubViewController()

//将导航视图控制器对像作为当前窗口的根视图控制器

let navigationController = UINavigationController(rootViewController:viewContorller)

self.window?.rootViewController = navigationController

返回true

}

 

你可能感兴趣的:(学习笔记)