发现之前用创建一个空image赋值给UINavigationBar的shadowImage的方法不管用,那就给UINavigationBar的shadowImage设置一个透明的image或者和导航栏一样颜色的图片。代码如下:
// 创建一个UINavigationBar的category,实现以下方法:
//宏定义宽高
#define NavBar_width [UIScreen mainScreen].bounds.size.width
#define NavBar_height 1
//用导航栏图片设置
-(void)clearShadowImageWithImage:(UIImage*)image {
UIGraphicsBeginImageContext(CGSizeMake(NavBar_width, NavBar_height));
[image drawInRect:CGRectMake (0, 0, NavBar_width, NavBar_height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext ();
self.shadowImage = image;
}
//用导航栏颜色设置
- (void)clearShadowImageWithColor:(UIColor*)color {
self.shadowImage = [self imageWithColor:color];
}
- (UIImage*)imageWithColor:(UIColor*)color {
if (!color) color = [UIColor clearColor];
UIGraphicsBeginImageContext(CGSizeMake(NavBar_width, NavBar_height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake (0 ,0 , NavBar_width, NavBar_height));
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
网友评论