更新:2018.05.24
整理了一下demo:SwiftDemo
-
UINavigationController
導(dǎo)航條控制器用于顯示多屏并具有一定層次結(jié)構(gòu)的內(nèi)容呜投,是構(gòu)建分層應(yīng)用的主要工具休弃。 - 它維護(hù)了一個(gè)視圖控制器棧,所有的子視圖控制器都處于堆棧中健田。
-
UINavigationController
導(dǎo)航條控制器在管理、切入和切出多個(gè)內(nèi)容頁方面淑仆,與UITabBarController
類似漆诽。 -
UINavigaitonController
和UITabBarController
區(qū)別在于:UINavigationController
是作為棧來實(shí)現(xiàn)的,它更適合用于處理和顯示分層的數(shù)據(jù)扮休。
一迎卤、創(chuàng)建一個(gè)UINavigationController
- 首先我們還是新建一個(gè)項(xiàng)目,然后添加兩個(gè)新的視圖控制器:
FirstViewController
和SecondViewController
玷坠。
- 為了更好的演示蜗搔,我們分別設(shè)置一下兩個(gè)視圖控制器的屬性
FirstViewController.swift
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "第一頁"
view.backgroundColor = UIColor.gray
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "下一頁", style: .plain, target: self, action: #selector(nextPageClick))
}
func nextPageClick() {
navigationController?.pushViewController(SecondViewController(), animated: true)
}
}
SecondViewController
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "第二頁"
view.backgroundColor = UIColor.cyan
}
}
a. 在FirstViewController
中,我們設(shè)置了title
和背景顏色八堡,title
是這個(gè)視圖控制器的標(biāo)題樟凄,如果有導(dǎo)航條的話,這個(gè)title
會(huì)顯示在導(dǎo)航條的中心位置兄渺,如果沒有導(dǎo)航條的話缝龄,它就沒什么作用,
b. 接著,我們創(chuàng)建了一個(gè)導(dǎo)航條右側(cè)的按鈕,確切的說叔壤,是重建了一個(gè)UIBarButtonItem
瞎饲,并替換了默認(rèn)的導(dǎo)航條右側(cè)按鈕。添加方法炼绘,點(diǎn)擊這個(gè)按鈕嗅战,調(diào)起nextPageClick ()
方法。
c. 當(dāng)點(diǎn)擊rightBarButtonItem
時(shí)俺亮,執(zhí)行nextPageClick()
驮捍,然后當(dāng)前視圖控制器的導(dǎo)航控制器(navigationController)
調(diào)起pushViewController()
方法,目的是跳轉(zhuǎn)到下一個(gè)視圖控制器铅辞。pushViewController()
有兩個(gè)參數(shù)厌漂,第一個(gè)參數(shù)表示需要加載的視圖控制器,第二個(gè)參數(shù)表示是否需要?jiǎng)赢嬓Ч迳骸.?dāng)然苇倡,這個(gè)方法只有在視圖控制器的導(dǎo)航控制器被實(shí)例化之后才有效。
d. 在SecondViewController.swift
中囤踩,我們只設(shè)置了title
和背景顏色旨椒,當(dāng)我們跳轉(zhuǎn)到這個(gè)視圖控制器,導(dǎo)航控制器會(huì)默認(rèn)生成一個(gè)leftBarButton
返回按鈕堵漱。
- 接下來综慎,我們到
AppDelegate.swift
中,將FirstViewController
設(shè)置為導(dǎo)航控制器的跟視圖控制器:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window?.rootViewController = UINavigationController(rootViewController: FirstViewController())
return true
}
二勤庐、UINavigationController視圖入棧和出棧
在上面我們創(chuàng)建了一個(gè)簡單的導(dǎo)航控制器示惊,這個(gè)導(dǎo)航控制器有一個(gè)根視圖控制器,當(dāng)點(diǎn)擊rightBarButtonItem
時(shí)愉镰,SecondViewController
會(huì)被push到導(dǎo)航控制器中米罚。
- 這里的push就相當(dāng)于入棧操作。
- 與之相反的pop操作丈探,相當(dāng)于出棧录择。
- 根據(jù)堆棧的先進(jìn)先出規(guī)則,當(dāng)執(zhí)行pop操作時(shí)碗降,位于棧頂?shù)囊晥D控制器隘竭,將被從導(dǎo)航控制器的堆棧中移除。
這里就不實(shí)際舉例說明了讼渊,比較好理解动看。
導(dǎo)航欄的顯示和隱藏
我們可以在viewWillAppear()
中設(shè)置視圖控制器的navigationController
的navigationBarHidden
屬性來設(shè)置導(dǎo)航欄的隱藏和顯示。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
a. navigationBarHidden ()
方法有兩個(gè)參數(shù)爪幻,第一個(gè)參數(shù)表示是否隱藏導(dǎo)航欄弧圆,第二個(gè)屬性表示是否需要?jiǎng)赢嬓Ч?/p>
三赋兵、導(dǎo)航欄樣式
根據(jù)項(xiàng)目的需求,原始的導(dǎo)航欄很難滿足我們的需求搔预。
// 添加導(dǎo)航條上方的提示霹期,這個(gè)屬性被設(shè)置后,導(dǎo)航欄的高度將增加到74
navigationItem.prompt = "正在使用您的位置..."
// 設(shè)置導(dǎo)航欄不透明
navigationController?.navigationBar.isTranslucent = false
// 設(shè)置導(dǎo)航條樣式
navigationController?.navigationBar.barStyle = UIBarStyle.black
// 設(shè)置導(dǎo)航欄關(guān)鍵元素顏色拯田,tintColor屬性是View專門用來指定所包含的關(guān)鍵元素的顏色
navigationController?.navigationBar.tintColor = UIColor.white
// 設(shè)置導(dǎo)航欄背景圖片
navigationController?.navigationBar.setBackgroundImage(UIImage(named:""), for: .any, barMetrics: .default)
// 設(shè)置導(dǎo)航欄 title 的 Font
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 18)]
// 設(shè)置導(dǎo)航欄 title 的 Color
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
// 設(shè)置導(dǎo)航欄陰影部分圖片历造,也就是導(dǎo)航欄下面那條細(xì)線
navigationController?.navigationBar.shadowImage = UIImage(named:"")
// 設(shè)置導(dǎo)航欄陰影顏色
navigationController?.navigationBar.layer.shadowColor = UIColor.green.cgColor
// 設(shè)置導(dǎo)航欄陰影偏移
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 10)
// 設(shè)置導(dǎo)航欄陰影透明度
navigationController?.navigationBar.layer.shadowOpacity = 0.2
- 設(shè)置prompt屬性之后,導(dǎo)航欄的高度會(huì)增加到74船庇,并在導(dǎo)航欄的上方顯示內(nèi)容吭产。
- translucent屬性默認(rèn)是 true ,當(dāng)設(shè)置了該屬性后,導(dǎo)航欄變成不透明鸭轮,同時(shí)臣淤,視圖控制器的view的高度也將發(fā)生變化。
- 導(dǎo)航欄也可以設(shè)置背景圖片窃爷,通過setBackgroundImage()方法邑蒋,該方法有三個(gè)參數(shù),第一個(gè)參數(shù)代表圖片按厘,第二個(gè)參數(shù)和第三個(gè)參數(shù)都是枚舉医吊,第二個(gè)參數(shù)表示導(dǎo)航欄的位置,第三個(gè)參數(shù)表示導(dǎo)航欄的外觀逮京。具體可以自己試一下卿堂。
- titleTextAttributes屬性是一個(gè)字典,一般我們用于設(shè)置導(dǎo)航欄標(biāo)題的文本大小和字體懒棉。
- 導(dǎo)航欄的陰影部分是可以設(shè)置的草描,包括更改陰影部分的顏色、背景策严、大小等穗慕。
四、導(dǎo)航控制器和標(biāo)簽控制器
在iOS開發(fā)過程中享钞,經(jīng)常會(huì)將navigationCongtroller
和tabBarController
相結(jié)合使用揍诽,我們可以回到Swift(十三)UITabBarController的仿微信的例子诀蓉,來看一下栗竖。
直接在AppDelegate.swift
文件中修改即可。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let wechat = WeChatViewController()
// 未選中狀態(tài)Tab圖片
wechat.tabBarItem.image = UIImage(named: "tab1")?.withRenderingMode(.alwaysOriginal)
// 選中狀態(tài)Tab圖片
wechat.tabBarItem.selectedImage = UIImage(named: "selectTab1")?.withRenderingMode(.alwaysOriginal)
let addressBook = AddressBookViewController()
addressBook.tabBarItem.image = UIImage(named: "tab2")?.withRenderingMode(.alwaysOriginal)
addressBook.tabBarItem.selectedImage = UIImage(named: "selectTab2")?.withRenderingMode(.alwaysOriginal)
let find = FindViewController()
find.tabBarItem.image = UIImage(named: "tab3")?.withRenderingMode(.alwaysOriginal)
find.tabBarItem.selectedImage = UIImage(named: "selectTab3")?.withRenderingMode(.alwaysOriginal)
let mine = MineViewController()
mine.tabBarItem.image = UIImage(named: "tab4")?.withRenderingMode(.alwaysOriginal)
mine.tabBarItem.selectedImage = UIImage(named: "selectTab4")?.withRenderingMode(.alwaysOriginal)
let tabBarController = UITabBarController()
tabBarController.tabBar.tintColor = UIColor.init(colorLiteralRed: 9/255.0, green: 187/255.0, blue: 7/255.0, alpha: 1)
wechat.title = "微信"
let wechatNavigationController = UINavigationController(rootViewController: wechat)
addressBook.title = "通訊錄"
let addressBookNavigationController = UINavigationController(rootViewController: addressBook)
find.title = "發(fā)現(xiàn)"
let findNavigaitonController = UINavigationController(rootViewController: find)
mine.title = "我的"
let mineNavigationController = UINavigationController(rootViewController: mine)
tabBarController.viewControllers = [wechatNavigationController,addressBookNavigationController,findNavigaitonController,mineNavigationController]
window?.rootViewController = tabBarController
return true
}
a. 我們可以看到渠啤,現(xiàn)在TabBar的viewControllers
里面添加的都是UINavigationController
了狐肢。