Swift-導(dǎo)航控制器

AppDelegate.swift代碼如下:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame:UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.white
        self.window?.makeKeyAndVisible()
        //1.創(chuàng)建導(dǎo)航控制器的根視圖
        let vc = ViewController()
        //2.創(chuàng)建導(dǎo)航視圖控制器,并為他制定根視圖控制器
        let navigation = UINavigationController(rootViewController: vc)
        //3.將導(dǎo)航視圖控制器設(shè)置為window的根視圖控制器
        self.window?.rootViewController = navigation


        return true
    }

ViewController.swift代碼如下:

import UIKit

class ViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupNavigationBar()
    }
    func setupNavigationBar() {
        //UINavigationController 配置他的時(shí)候設(shè)置只是當(dāng)前視圖控制器上導(dǎo)航條外觀
//        self.title = "撥號(hào)"
        self.navigationItem.title = "消息"
        //設(shè)置title顏色字體
        let dic = [NSFontAttributeName:UIFont.systemFont(ofSize: 30.0),NSForegroundColorAttributeName:UIColor.red]

        self.navigationController?.navigationBar.titleTextAttributes = dic
        //創(chuàng)建按鈕對(duì)象
        let rightButton = UIBarButtonItem(title: "跳轉(zhuǎn)", style: .plain, target: self, action: #selector(rightButtonAction))
        //給右側(cè)rightBarButtonItem賦值
        self.navigationItem.rightBarButtonItem = rightButton
        //配置左側(cè)的leftBarButtonItem
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(leftButtonAction))

        //UINavigationBar 配置他的時(shí)候所有視圖都會(huì)顯示,公共的屬性,此時(shí)是通過導(dǎo)航視圖控制器來設(shè)置
        self.navigationController?.isNavigationBarHidden = false
        //bar顏色
//        self.navigationController?.navigationBar.barStyle = .black
        //bar背景顏色
        self.navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
        //導(dǎo)航條yans
        self.navigationController?.navigationBar.barTintColor = UIColor.yellow
        //設(shè)置控件顏色
        self.navigationController?.navigationBar.tintColor = UIColor.black
    }
    //設(shè)置leftButtonAction的關(guān)聯(lián)方法
    func leftButtonAction() {
        print("左側(cè)按鈕")
    }
    //設(shè)置rightButtonAction的關(guān)聯(lián)方法
    func rightButtonAction() {
        let redVC = RedViewController()
        self.navigationController?.pushViewController(redVC, animated: true)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

RedViewController.swift代碼如下:

import UIKit

class RedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
        self.setupNavigationBar()
//        self.navigationController?.isNavigationBarHidden = true
        //導(dǎo)航條的毛玻璃效果,半透明效果
        //導(dǎo)航欄半透明效果(IOS7以后 默認(rèn)YES)
        //當(dāng)半透明效果開啟時(shí) 屏幕左上角為坐標(biāo)原點(diǎn)
        //關(guān)閉時(shí),導(dǎo)航欄左下角為坐標(biāo)原點(diǎn)
        self.navigationController?.navigationBar.isTranslucent = false
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 150, height: 100)
        button.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        button.setTitle("返回", for: .normal)
        self.view.addSubview(button)

        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

    }
    //button關(guān)聯(lián)事件
    func buttonAction(){
        self.navigationController?.isNavigationBarHidden = false
        self.navigationController?.popViewController(animated: true)
    }
    func setupNavigationBar() {
        let left1 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(leftAction))
        let left2 = UIBarButtonItem(barButtonSystemItem: .camera,target:self, action:#selector(left2Action))
        self.navigationItem.leftBarButtonItems = [left1,left2]

        let titleView = UISegmentedControl(items: ["消息","電話"])
        titleView.frame = CGRect(x:0,y:0,width:100,height:30)
        titleView.selectedSegmentIndex = 0
        //給navigationItem和titleView屬性賦值
        self.navigationItem.titleView = titleView




    }
    //left的關(guān)聯(lián)方法
    func leftAction(sender:UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)

    }
    //left2的關(guān)聯(lián)方法
    func left2Action(sender:UIBarButtonItem) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容