iOS开发小结 - UINavigationBar设置shadowImage
2016-07-11 23:35
537 查看
在项目中我们经常用到UINavigationBar,有时候我们需要设置UINavigationBar设置shadowImage,把下面的小黑条给弄掉,或者换一个颜色的阴影条,UINavigationBar有一个属性是shadowImage,然而发现设置了并没有用,下面描述一下怎么正确使用shadowImage属性。
我们先设置一下shadowImage为蓝色的图片,下面是代码:
let size = self.navigationController!.navigationBar.frame.size UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale) var context = UIGraphicsGetCurrentContext() UIColor.blueColor().setFill() CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, 2)) CGContextDrawPath(context, .Fill) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.navigationController?.navigationBar.shadowImage = image
设置完成后发现并没有效果,那是因为我们没有设置backgroundImage,我们先看一下apple官方对于这个属性的解释:
/* Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forBarMetrics: (if the default background image is used, the default shadow image will be used).
大概意思就是像让shadowImage有作用,必须先设置backgroundImage,下面我们再实现一下一下代码,先设置backgroundImage为红色,然后再设置shadowImage:
let size = self.navigationController!.navigationBar.frame.size UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale) var context = UIGraphicsGetCurrentContext() UIColor.blueColor().setFill() CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, 2)) CGContextDrawPath(context, .Fill) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.navigationController?.navigationBar.shadowImage = image
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.width+20), false, UIScreen.mainScreen().scale)
context = UIGraphicsGetCurrentContext()
UIColor.redColor().setFill()
CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height+20))
CGContextDrawPath(context, .Fill)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
这下发现起作用了,同样的想要影藏navBar下面的小黑线,只需要将设置shadowImage成一个空的UIImage就行了:
self.navigationController?.navigationBar.shadowImage = UIImage() UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.width+20), false, UIScreen.mainScreen().scale) var context = UIGraphicsGetCurrentContext() UIColor.whiteColor().setFill() CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height+20)) CGContextDrawPath(context, .Fill) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
总结:想要设置shadowImage必须要先设置navigationBar的backgroundImage。
相关文章推荐
- Vue.js学习 Item10 – 过渡与动画
- UESTC 2016 Summer Training #1 Div.2
- JAVA UUID 生成
- STL序列式容器之队列——queue
- Android中的JUnit和Java中的JUit
- STL序列式容器之双端队列——deque
- NYOJ 95 众数 map找value的最值
- Integer.parseInt() 和 valueOf()
- volley的使用【定制自己的request】(三)
- <item name="android:windowNoTitle">true</item> 不起作用。标题栏未被隐藏
- iOS UI常用控件
- Easy UI DataGrid 与 分页
- 树状数组_poj_2299_Ultra-QuickSort
- TableView使用Nib文件出现的错误Assertion failure in dequeue
- UI调试神器 for ios:Reveal的使用与破解
- require实现单页应用程序(SPA)
- UGUI之Text镜像效果
- UI调试神器 for ios:Reveal的使用与破解
- 【Xamarin报错】AndroidManifest.xml : warning XA0101: @(Content) build action is not supported
- android service 的动态更改UI和service重启问题