有时候系统自带的 TabBar 或 NavigationBar 分割线颜色和我们App里设计的不一致,此时就需要自定义系统的分割线颜色。
修改 TabBar 分割线颜色
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = tabBarShadowImage()
func tabBarShadowImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.width(), height: 0.5), false, 0)
let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: UIScreen.width(), height: 0.5))
UIColor.red.setFill()// 自定义TabBar分割线颜色
path.fill()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
修改 NavigationBar 分割线颜色
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = navBarShadowImage()
func navBarShadowImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.width(), height: 0.5), false, 0)
let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: UIScreen.width(), height: 0.5))
UIColor.blue.setFill()// 自定义NavigationBar分割线颜色
path.fill()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}