先來(lái)介紹一下導(dǎo)航條的基礎(chǔ)知識(shí):
一厕隧、UINavgationController-導(dǎo)航控制器
我們先來(lái)看一段簡(jiǎn)單的代碼:
//在AppDelegate中創(chuàng)建導(dǎo)航控制器,并設(shè)置它為根視圖
let root:RootViewController = RootViewController()
let nav = UINavigationController(rootViewController: root)
self.window!.rootViewController = nav
//在RootViewController中設(shè)置標(biāo)題(title)和item
self.title = "第一頁(yè)"
let nextItem = UIBarButtonItem(title: "下一頁(yè)", style:.Plain, target: self, action: #selector(RootViewController.nextPage))
self.navigationItem.rightBarButtonItem = nextItem
//方法實(shí)現(xiàn)
func nextPage()
//推出下一頁(yè)
let vc = ViewController()
svc.delegate = self
self.navigationController!.pushViewController(vc, animated: true)
}
在這段簡(jiǎn)單的代碼中矫限,主要實(shí)現(xiàn)了設(shè)置導(dǎo)航控制器為根視圖法绵,并實(shí)現(xiàn)推出下一頁(yè)的功能。當(dāng)然我們還可以對(duì)導(dǎo)航控制器的導(dǎo)航條UINavgationBar進(jìn)行屬性設(shè)置:
//設(shè)置navgationBar的顏色
nav.navigationBar.barTintColor = UIColor.clearColor()
//關(guān)閉navigationBar的毛玻璃效果
/***在設(shè)置背景圖片的時(shí)候會(huì)出現(xiàn)與圖片顏色不對(duì)的情況涧狮,只要將translucent設(shè)置成false就OK啦***/
nav.navigationBar.translucent = false
關(guān)于navgationBar的圖片設(shè)置簡(jiǎn)單說(shuō)明一下:
-
不同尺寸的圖片效果不同:
1.320 * 44,只會(huì)給navigationBar附上圖片
2.高度小于44,以及大于44且小于64:會(huì)平鋪navigationBar以及狀態(tài)條上顯示
3.高度等于64:整個(gè)圖片在navigationBar以及狀態(tài)條上顯示nav.navigationBar.setBackgroundImage(UIImage(named: "background"), forBarMetrics:.Compact) //它有四種狀態(tài)Default,Compact,CompactPrompt,DefaultPrompt炕矮,大家可以根據(jù)自己的需求來(lái)修改
常用的也就是這些,如有不足大家可以在評(píng)論上說(shuō)明者冤,大臉貓會(huì)及時(shí)添加的哦肤视。
二、push和pop
在前面那段簡(jiǎn)單的代碼中小伙伴們已經(jīng)看到了涉枫,大臉貓用到了push邢滑,實(shí)現(xiàn)了從RootViewController到ViewController的跳轉(zhuǎn)。下面我們來(lái)詳細(xì)的介紹一下push和pop的用法愿汰。
push:
self.navigationController?.pushViewController(viewController, animated: true)
我先來(lái)介紹一個(gè)每個(gè)參數(shù):
- 參數(shù)1:你要push到的控制器
- 參數(shù)2:是否活動(dòng)殊鞭,這個(gè)參數(shù)一般情況下都是true
pop:
//返回到上一個(gè)頁(yè)面
self.navigationController?.popViewControllerAnimated(animated: true)
//返回到指定頁(yè)面
self.navigationController?.popToViewController(root, animated: true)
//返回到根視圖控制器
self.navigationController?.popToRootViewControllerAnimated(true)
三、UIBarButtonItem
細(xì)心的小伙伴們應(yīng)該會(huì)發(fā)現(xiàn)尼桶,在上面那段代碼中操灿,我已經(jīng)使用過(guò)一次UIBarButtonItem了,代碼是這個(gè)樣子的:
let nextItem = UIBarButtonItem(title: "下一頁(yè)", style:.Plain, target: self, action: #selector(RootViewController.nextPage))
self.navigationItem.rightBarButtonItem = nextItem
這兩行代碼實(shí)現(xiàn)了UIBarButtonItem的創(chuàng)建和事件的添加泵督,還有就是設(shè)置它右邊的item趾盐。
我們?cè)賮?lái)看看其他的創(chuàng)建方式:
//帶文字
let barBtn = UIBarButtonItem(title: "next", style: .Done, target: self, action: #selector(RootViewController.nextPage))
//帶背景圖片
let barBtn = UIBarButtonItem(image: UIImage(named: "background"), style: .Done, target: self, action: #selector(RootViewController.nextPage))
//帶背景圖片和手機(jī)風(fēng)景圖片
let barBtn = UIBarButtonItem(image: UIImage(named: "background"), landscapeImagePhone: UIImage(named: "background_1"), style: .Done, target: self, action: #selector(RootViewController.nextPage))
//設(shè)置位置在左邊還是在右邊
self.navigationItem.rightBarButtonItem = barBtn
self.navigationItem.leftBarButtonItem = barBtn
//自定義視圖
let barButton = UIBarButtonItem(customView: button)
四、UINavgationController的代理方法
導(dǎo)航條也是有代理的哦小腊,但是用的不是很多救鲤,我們來(lái)了解一下
self.navigationController?.delegate = self;//設(shè)置代理
//代理方法
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
}
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
}
簡(jiǎn)單的基礎(chǔ)知識(shí)就先介紹到這里,后面有時(shí)間還會(huì)發(fā)布有關(guān)導(dǎo)航條側(cè)滑的實(shí)現(xiàn)和導(dǎo)航條的其他功能秩冈,( _ )/~~拜拜