導航欄UINavigationController

1,導航欄上面添加按鈕

1.1,根據文字添加
 let rightBarBtn = UIBarButtonItem.init(title: "right", style: 
 UIBarButtonItemStyle.plain, target: self, action: #selector(action_riht))
self.navigationItem.rightBarButtonItem = rightBarBtn

//1.2,系統(tǒng)樣式添加     
let leftBarBtn = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(action_left))      
 //1.3,圖片方式添加

let image = UIImage(named:"icon_iPad-29")?.withRenderingMode(.alwaysOriginal)
let leftBarBtn2 = UIBarButtonItem.init(image: image, style: UIBarButtonItemStyle.done, target: self, action: #selector(action_left))
self.navigationItem.leftBarButtonItem = leftBarBtn2
//        self.navigationItem.leftBarButtonItems = [leftBarBtn,leftBarBtn2]

2,設置導航欄的背景顏色

self.navigationController?.navigationBar.barTintColor = UIColor.brown

3,設置導航欄按鈕字體顏色

 self.navigationController?.navigationBar.tintColor = UIColor.cyan

4,設置標題樣式與顏色(通過導航欄字典的方式)

self.navigationController?.navigationBar.titleTextAttributes = 
[NSAttributedStringKey.foregroundColor:
UIColor.yellow,NSAttributedStringKey.font:UIFont.systemFont(ofSize: 20)]

5,設置返回按鈕的圖片,不影響側滑返回

//        let image = UIImage(named:"icon_iPad-29")?.withRenderingMode(.alwaysOriginal)
//        let navBar = self.navigationController?.navigationBar
//        navBar?.backIndicatorImage = image;
//        navBar?.backIndicatorTransitionMaskImage = image;

6,設置導航欄的背景圖片

        self.navigationController?.navigationBar
        .setBackgroundImage(UIImage.init(named: "icon_iPad-29"), for: UIBarMetrics.default)

7,清除導航欄下面的線條

        self.navigationController?.navigationBar.shadowImage = UIImage.init()
        let isTest = false
        if isTest {
8,隱藏導航欄
            self.navigationController?.navigationBar.isHidden = true
9,導航跳轉
            //9.1 壓棧
            let secondVC = SecondViewController.init()
            self.navigationController?.pushViewController(secondVC, animated: true)
            //9.2 出棧
            //出棧到上級界面
            self.navigationController?.popViewController(animated: true)
            //出棧到頂端
            self.navigationController?.popToRootViewController(animated: true);
            
        }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容