刚刚看了 iOS --没事撸一撸控件之UINavigationBar 这篇文章, 其中提到了 UINavigationBar
的滑动渐变效果和去除底部横线的方法, 深有其感, 现提供另一种简单的方法来实现.
其实也没有什么 special 啦, 就是通过颜色来生成一个UIImage, 设置成背景图片即可.
通过颜色来生成一张纯色的图片:
func imageFromColor(color:UIColor)->UIImage{
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContext(rect.size);
let context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
使用的时候:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let random = CGFloat(arc4random() % 9) / 10
let color = UIColor(red: 0, green: 120 / 255, blue: 106 / 255, alpha: random)
let a = NSDate().timeIntervalSince1970
self.navigationController?.navigationBar.setBackgroundImage(imageFromColor(color), forBarMetrics: UIBarMetrics.Default)
print(NSDate().timeIntervalSince1970 - a)//0.00041508674621582
}
效果图:
至于去除底部那一条难看的横线, 恩...其实一行代码就搞定了的....那就是:
self.navigationController?.navigationBar.shadowImage = UIImage()
效果图:
预祝各位挖坑愉快.
网友评论