核心方法:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//消除navigationBar 与tableView 之间的线条
let bar = self.navigationController?.navigationBar
bar?.translucent = false
bar?.setBackgroundImage(UIImage(named: "navBackImage"), forBarMetrics: UIBarMetrics.Default)
bar?.shadowImage = XRUIImage.imageWithColor(XRMacro.UIColorFromRGB(0x51b4bd), size: CGSizeMake(XRMacro.KSCREEN_WIDTH(), 3))
}
UIimage扩展方法:
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
本文介绍如何在Swift中通过重写viewWillAppear方法来调整UINavigationController的navigationBar样式,包括设置背景图片、阴影颜色等,以实现自定义的导航栏外观。

被折叠的 条评论
为什么被折叠?



