自定义UINavigationBar的背景色

来源:互联网 发布:喝水 知乎 编辑:程序博客网 时间:2024/04/29 13:25

新建.h和.m文件。重写UINavigationBar


#import<UIKit/UIKit.h>


@interface UINavigationBar (UINavigationBarCategory) 


UIImageView *backgroundView;


- (void)setBackgroundImage:(UIImage*)image;

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;


@end



@implementation UINavigationBar (UINavigationBarCategory)


-(void)setBackgroundImage:(UIImage*)image

{

   if(image ==nil)

{

[backgroundViewremoveFromSuperview];

}

else

{

backgroundView = [[UIImageViewalloc]initWithImage:image];

backgroundView.frame = CGRectMake(0.f,0.f,self.frame.size.width,self.frame.size.height);

backgroundView.autoresizingMask  = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[selfaddSubview:backgroundView];

[selfsendSubviewToBack:backgroundView];

[backgroundViewrelease];

}

}


其中UITabBar也可用此方法来自定义背景。