UINavigationItem基礎(chǔ)
//導(dǎo)航條 -> 屬性導(dǎo)航控制器
//navigationItem -> 讓導(dǎo)航控制器管理的視圖控制器
//創(chuàng)建window
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
let navC = YTNavigationController(rootViewController: OneViewController())
self.window?.rootViewController = navC
//將狀態(tài)欄變成白色(默認(rèn)是黑色)
//1.在info.plist文件中去添加一個鍵值對 -> View controller-based status bar appearance:NO
//2.通過應(yīng)用程序?qū)ο蟾淖儬顟B(tài)欄的樣式
//a.拿到當(dāng)前應(yīng)用程序?qū)ο? let app = UIApplication.sharedApplication()
//b.設(shè)置狀態(tài)欄的樣式
//LightContent -> 白色
app.statusBarStyle = .LightContent
//定制導(dǎo)航條
//1.設(shè)置背景顏色
self.navigationBar.barTintColor = UIColor.blackColor()
//2.設(shè)置填充顏色
self.navigationBar.tintColor = UIColor.whiteColor()
func navigationItemSetting() {
//1.定制rightItem
//a.通過其他視圖來創(chuàng)建一個UIBarButtonItem對象
let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
btn.setTitle("注冊", forState: .Normal)
btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)
let item1 = UIBarButtonItem.init(customView: btn)
//b.通過一張圖片創(chuàng)建一個UIBarButtonItem對象
//參數(shù)1:圖片
//參數(shù)2:item的風(fēng)格
//參數(shù)3:調(diào)用方法的對象
//參數(shù)4:當(dāng)當(dāng)前item對應(yīng)的按鈕被點擊后會調(diào)用的方法
let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
//c.通過文字去創(chuàng)建一個UIBarButtonItem對象
let item3 = UIBarButtonItem.init(title: "注冊", style: .Plain, target: self, action: "itemAction:")
//d.通過系統(tǒng)樣式去創(chuàng)建UIBarButtonItem對象
//參數(shù)1:指定的系統(tǒng)樣式
let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")
//2.設(shè)置navigationBar右邊的顯示
//a.在右邊顯示一個視圖
self.navigationItem.rightBarButtonItem = item4
//b.在右邊顯示顯示多個視圖
//self.navigationItem.rightBarButtonItems = [item4,item3]
//3.設(shè)置navigationBar左邊的顯示
self.navigationItem.leftBarButtonItem = item2
//self.navigationItem.leftBarButtonItems = [item1, item2]
//4.設(shè)置navigationBar中間的顯示
//a.顯示純文字
self.title = "登錄"
//b.通過一個label顯示文字
let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
label.text = "登錄"
label.textColor = UIColor.yellowColor()
label.textAlignment = .Center
//c.創(chuàng)建中間顯示的分段選中器
let segement = UISegmentedControl.init(items: ["海賊","火影"])
segement.frame = CGRectMake(0, 0, 100, 40)
segement.selectedSegmentIndex = 0
//設(shè)置中間視圖
self.navigationItem.titleView = segement
//設(shè)置背景顏色
self.view.backgroundColor = UIColor.orangeColor()
//設(shè)置rightItem
let item = UIBarButtonItem.init(barButtonSystemItem: .Cancel, target: self, action: "itemAction")
self.navigationItem.rightBarButtonItem = item
//1.替換系統(tǒng)自帶的返回按鈕 -> 設(shè)置leftBarButtonItem屬性
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")
//self.navigationItem.leftBarButtonItem = item2
//2.隱藏系統(tǒng)自帶的返回按鈕
self.navigationItem.hidesBackButton = true
toolBar
//將狀態(tài)欄變成白色(默認(rèn)是黑色)
//1.在info.plist文件中去添加一個鍵值對 -> View controller-based status bar appearance:NO
//2.通過應(yīng)用程序?qū)ο蟾淖儬顟B(tài)欄的樣式
//a.拿到當(dāng)前應(yīng)用程序?qū)ο? let app = UIApplication.sharedApplication()
//b.設(shè)置狀態(tài)欄的樣式
//LightContent -> 白色
app.statusBarStyle = .LightContent
//1.定制toolBar(高度是44)
//toolBar屬于導(dǎo)航控制器,默認(rèn)是隱藏的
//a.讓toolBar顯示出來
self.toolbarHidden = false
//b.設(shè)置是否有透明度(默認(rèn)true->有透明度)
self.toolbar.translucent = false
//c.設(shè)置背景顏色
self.toolbar.barTintColor = UIColor.yellowColor()
//d.設(shè)置填充顏色
self.toolbar.tintColor = UIColor.redColor()
//2.定制導(dǎo)航條
//a.設(shè)置背景顏色
self.navigationBar.barTintColor = UIColor.blackColor()
//b.設(shè)置填充顏色
self.navigationBar.tintColor = UIColor.whiteColor()
class OneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.greenColor()
//1.定制navigationItem
self.navigationItemSetting()
//2.定制toolBarItem
self.toolBarItemSetting()
}
}
//MARK: - 定制toolBarItem
extension OneViewController{
func toolBarItemSetting() {
//創(chuàng)建toolBar上顯示的按鈕對應(yīng)的item
let item1 = UIBarButtonItem.init(barButtonSystemItem: .Camera, target: self, action: "toolBarAction")
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "toolBarAction")
let item3 = UIBarButtonItem.init(barButtonSystemItem: .Edit, target: self, action: "toolBarAction")
let item4 = UIBarButtonItem.init(barButtonSystemItem: .Refresh, target: self, action: "toolBarAction")
//a.FlexibleSpace(相當(dāng)于空格,用來設(shè)置每個item之間的間距,間距是自動計算的)
let space1 = UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: "")
//b.FixedSpace(相當(dāng)于空格,用來設(shè)置每個item之間的間距,間距需要手動設(shè)置)
let space2 = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: "")
//設(shè)置間距
space2.width = 20
//將item添加到toolBar上
self.toolbarItems = [space2,item1,space2,item2,space2,item3,space2,item4,space2]
}
func toolBarAction() {
print("toolBar被點擊")
}
}
//MARK: - 定制navigationItem
extension OneViewController{
func navigationItemSetting() {
//1.定制rightItem
//a.通過其他視圖來創(chuàng)建一個UIBarButtonItem對象
let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
btn.setTitle("注冊", forState: .Normal)
btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)
let item1 = UIBarButtonItem.init(customView: btn)
//b.通過一張圖片創(chuàng)建一個UIBarButtonItem對象
//參數(shù)1:圖片
//參數(shù)2:item的風(fēng)格
//參數(shù)3:調(diào)用方法的對象
//參數(shù)4:當(dāng)當(dāng)前item對應(yīng)的按鈕被點擊后會調(diào)用的方法
let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
//c.通過文字去創(chuàng)建一個UIBarButtonItem對象
let item3 = UIBarButtonItem.init(title: "注冊", style: .Plain, target: self, action: "itemAction:")
//d.通過系統(tǒng)樣式去創(chuàng)建UIBarButtonItem對象
//參數(shù)1:指定的系統(tǒng)樣式
let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")
//2.設(shè)置navigationBar右邊的顯示
//a.在右邊顯示一個視圖
self.navigationItem.rightBarButtonItem = item4
//b.在右邊顯示顯示多個視圖
//self.navigationItem.rightBarButtonItems = [item4,item3]
//3.設(shè)置navigationBar左邊的顯示
self.navigationItem.leftBarButtonItem = item2
//self.navigationItem.leftBarButtonItems = [item1, item2]
//4.設(shè)置navigationBar中間的顯示
//a.顯示純文字
self.title = "登錄"
//b.通過一個label顯示文字
let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
label.text = "登錄"
label.textColor = UIColor.yellowColor()
label.textAlignment = .Center
//c.創(chuàng)建中間顯示的分段選中器
let segement = UISegmentedControl.init(items: ["海賊","火影"])
segement.frame = CGRectMake(0, 0, 100, 40)
segement.selectedSegmentIndex = 0
//設(shè)置中間視圖
self.navigationItem.titleView = segement
}
func itemAction(item:UIBarButtonItem) {
print("item被點擊")
//跳轉(zhuǎn)到下一個界面
let two = TwoViewController()
self.navigationController?.pushViewController(two, animated: true)
}
//按鈕點擊
func btnAction() {
print("跳轉(zhuǎn)到注冊頁")
}
}
/設(shè)置背景顏色
self.view.backgroundColor = UIColor.orangeColor()
//設(shè)置rightItem
let item = UIBarButtonItem.init(barButtonSystemItem: .Cancel, target: self, action: "itemAction")
self.navigationItem.rightBarButtonItem = item
//1.替換系統(tǒng)自帶的返回按鈕 -> 設(shè)置leftBarButtonItem屬性
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")
//self.navigationItem.leftBarButtonItem = item2
//2.隱藏系統(tǒng)自帶的返回按鈕
self.navigationItem.hidesBackButton = true
//定制navigationBar
//1.設(shè)置背景顏色
self.navigationBar.barTintColor = UIColor.blackColor()
//2.設(shè)置中間的文字顏色
//NSForegroundColorAttributeName 文字顏色的key
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
//3.設(shè)置填充顏色
self.navigationBar.tintColor = UIColor.whiteColor()