首页 > 代码库 > 自定义导航栏图片 & 适配 (类别)
自定义导航栏图片 & 适配 (类别)
#import "UINavigationBar+CustomImage.h"
@implementation UINavigationBar (CustomImage)
-(UIImage *)drawImage:(NSString *)imageName
{
UIImage *image = [UIImageimageNamed:imageName];
CGSize ssySize = CGSizeMake(320, (IOS_VERSION_7)?64:44);
UIGraphicsBeginImageContext(ssySize);
[image drawInRect:CGRectMake(0, 0, ssySize.width, ssySize.height)];
UIImage *reImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reImage;
}
- (UIImage *)barBackground
{
return [UIImageimageNamed:@"live_bg_yellow.png"];
}
- (void)didMoveToSuperview
{
//iOS5 only
if ([selfrespondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[selfsetBackgroundImage:[selfdrawImage:@"live_bg_yellow.png"] forBarMetrics:UIBarMetricsDefault];
// 自定义导航栏字体颜色大小
[selfsetTitleTextAttributes:@{
NSForegroundColorAttributeName: [UIColorredColor],
NSFontAttributeName: [UIFontboldSystemFontOfSize:14],
}];
// [[UINavigationBar appearance] setTitleTextAttributes:@{
// NSForegroundColorAttributeName: [UIColor redColor],
// NSFontAttributeName: [UIFont boldSystemFontOfSize:14],
// }];
}
}
//this doesn‘t work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
//draw image
[[selfbarBackground] drawInRect:rect];
}
@end
自定义导航栏图片 & 适配 (类别)