Swift_ios_開發之UINavigationController的經常使用屬性那些事

1.導航欄是否隱藏

self.navigationController?.navigationBar.hidden = false

這裏要注意一點,navigationBar在頁面中是與基View平級的,因此若是導航欄一開始是隱藏的,當頁面加載完畢又想讓他顯示,這時候會發現基頁面總體向下移動了,而且原來的內容會有一部分顯示不完整!通過調試發現,若是不隱藏導航欄,基view的高度是屏幕高度與導航欄高度的差;若是隱藏了導航欄,基view的高度就是整個屏幕的高度。因此一開始隱藏了導航欄,頁面加載完成再顯示,就須要把基view的高度減去導航欄的高度!swift

2.導航欄的最底部顏色設置

//backgroundColor 是最底下的color 
self.navigationController?.navigationBar.backgroundColor = UIColor.redColor()

3.導航欄的表層顏色,即首先看到的顏色

//barTintColor 是表層顏色
self.navigationController?.navigationBar.barTintColor = UIColor.grayColor()

4.導航欄中間標題title的顏色,大小,字體設置

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blueColor(),NSFontAttributeName:UIFont(name: "Heiti SC", size: 24.0)!]

5.導航欄隱藏左邊backitem,即leftbarbuttonitem

//徹底隱藏backItem//
self.navigationItem.setHidesBackButton(true, animated: true)

6.導航欄leftbarbuttonitem的顏色設置

swiftself.navigationController?.navigationBar.tintColor = UIColor.whiteColor()

7.導航欄leftbarbuttonitem的字體,顏色,大小設置

self.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blueColor(),NSFontAttributeName: UIFont(name: "Chalkduster", size: 13)!], forState: UIControlState.Normal)

8.導航欄從新定義leftbarbuttonitem

//從新定義backItem,將覆蓋原來的BackItem.與storyboard中拖入一個item,效果同樣。都是覆蓋原來的backitem。

//第一種代碼定義方式
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "function"), animated: true)

//第二種代碼定義方式 
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "function")

//第三種代碼定義的方式
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(title: "<Grandre", style: UIBarButtonItemStyle.Plain, target: self, action: "function"), animated: true)

9.導航欄設置成透明

//將導航欄設置成透明
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true

若是對你有幫助,記得mark一下哦!ide


 

文/ChinaSwift(簡書做者)
原文連接:http://www.jianshu.com/p/738d9387ed12/comments/1185395
著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。字體

相關文章
相關標籤/搜索