- UINavigationController管理一個(gè)VC的棧,棧底的VC叫做這個(gè)UINavigationController的root view controller.
- 有一個(gè)函數(shù)叫做
popToRootViewController(animated:)
,就是返回rootVC如暖。 - UINavigationController在頂部有一個(gè)
navigationBar
擅憔,繼承自UIView。它的frame衡怀,bound及alpha值都不能改變。隱藏或者顯示這個(gè)navigationBar
要調(diào)用UINavigationController的setNavigationBarHidden(_:animated:)
方法。其中動(dòng)畫(huà)的時(shí)間是一個(gè)常量UINavigationControllerHideShowBarDuration
嗽元,不能改變。
To show or hide the navigation bar, you should always do so through the navigation controller by changing its isNavigationBarHidden property or calling the setNavigationBarHidden(_:animated:) method.
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBarHidden
-
navigationBar的各個(gè)屬性
NavBar的各個(gè)屬性 navigationBar的背景色
self.navigationController?.navigationBar.barTintColor = UIColor.yellowColor()
背景色
- navigationBar的配置
navigationBar有左喂击、中剂癌、右三個(gè)部分。顯示的具體內(nèi)容是根據(jù)當(dāng)前VC的UINavigationItem
屬性及棧中前一個(gè)VC的UINavigationItem
屬性配置的翰绊。 - UINavigationItem
UINavigationItem作為VC的屬性佩谷,在這個(gè)VC處于navigation stack時(shí),用來(lái)設(shè)置定于導(dǎo)航條的樣式等等监嗜。屬性分別用來(lái)設(shè)置導(dǎo)航條左谐檀、中、右的樣式等裁奇。
- 左邊
- leftBarButtonItem 最左邊的按鈕
- leftBarButtonItems 最左邊的按鈕們(可以有多個(gè))
- setLeftBarButtonItems(_:animated:)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "left", style: .Plain, target: self, action: #selector(TGVCButtom.leftButtonClicked(_:))) self.navigationItem.leftBarButtonItem?.tintColor = UIColor.brownColor() let dict = [NSForegroundColorAttributeName : UIColor.blueColor(), NSKernAttributeName: NSNumber.init(int: 10) self.navigationItem.leftBarButtonItem?.setTitleTextAttributes(dict, forState: .Normal) let sencondleftBarButtonItem = UIBarButtonItem.init(title: "leftSecond", style: .Plain, target: self, action: #selector(TGVCButtom.secondLeftButtonClicked(_:)))
self.navigationItem.setLeftBarButtonItems([self.navigationItem.leftBarButtonItem!,sencondleftBarButtonItem], animated: true)
```
左邊的按鈕們
- 中間
- title
self.navigationItem.title = "buttomVC"
- titleView 會(huì)覆蓋title桐猬。如果需要設(shè)置button、富文本可以考慮這個(gè)
> Custom title views are centered on the navigation bar and may be resized to fit
```
self.navigationItem.titleView = UIImageView.init(image: TGResizeImage(UIImage.init(named: "cat"), byRatio: 0.5) )
```
- 右邊刽肠。和左邊類似溃肪。
- rightBarButtonItem
- rightBarButtonItems
- setRightBarButtonItems(_:animated:)
-
返回按鈕
有時(shí)候既要有一個(gè)返回的按鈕,又要有一個(gè)左邊的按鈕音五。達(dá)到微信的網(wǎng)頁(yè)常有的返回+關(guān)閉的效果乍惊。
返回+關(guān)閉
假設(shè)一個(gè)當(dāng)前VC是A,push一個(gè)VC出去放仗。
self.navigationController?.pushViewController(self.topVC, animated:true)
那么需要
1. 設(shè)置自己的backBarButtonItem
self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "back", style: .Plain, target: nil, action: nil);
2. 設(shè)置topVC的leftBarButtonItem
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "leftButton", style: .Done, target: self, action: nil)
3. 設(shè)置topVC的兩個(gè)變量
self.navigationItem.hidesBackButton = false //返回按鈕沒(méi)有被隱藏
self.navigationItem.leftItemsSupplementBackButton = true //返回按鈕可以和左邊的按鈕一起出現(xiàn)
返回+關(guān)閉
- 奇怪的prompt
A single line of text displayed at the top of the navigation bar
self.navigationItem.prompt = "promt"http://不知道干嘛的润绎。。有啥用呢
奇怪的prompt
-
一個(gè)解釋
- 如何禁止左滑返回
self.navigationController?.interactivePopGestureRecognizer?.enabled = false