最近接到一个需求是要把NavBar的背景色以及1px的下划线颜色更换掉。
最先想到的方式就是放一张图片:
[navBar setBackgroundImage:[UIImage imageNamed:@"图片名称"]forBarMetrics:UIBarMetricsDefault];
但是这个感觉又不是那么好,所以不想用图片去作为nav的背景图
首先设置NavBar的背景颜色的方法是:
[navBarsetBarTintColor:[UIColorwhiteColor]];
其次,删除UINavigationBar下面一像素下划线的方法是:
navBar.shadowImage=[UIImage new];
这样的话下划线就删除了,修改下划线的颜色也是这个方法,但是必须生成一张纯色的图片放上去才可以
+(UIImage*)imageWithColor:(UIColor*)color size:(CGSize)size
{
CGRectrect =CGRectMake(0,0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorCGColor]);
CGContextFillRect(context, rect);
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
这样就大功告成了