说实话Google了一大圈,最后还是在Stack Overflow上找到了自己想要的答案,但是在swift里面还像没有imageWithColor方法,只能自定义了
下面是代码
func shadowImageLine() -> Void { self.navigationController?.navigationBar .setBackgroundImage(imageWithColor(UIColor.redColor()),forBarMetrics: UIBarMetrics.Default) self.navigationController?.navigationBar.shadowImage = UIImage() } func imageWithColor(color: UIColor) -> UIImage{ let rect = CGRectMake(0.0,0.0,1.0,1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context,color.CGColor) CGContextFillRect(context,rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }
这是最终的效果
原文链接:https://www.f2er.com/swift/323497.html