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
著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。字体

相关文章
相关标签/搜索