Objective-c代码

来源:互联网 发布:淘宝全屏导航栏颜色 编辑:程序博客网 时间:2024/06/06 20:26

Objective-c代码 //CustomNavigationBar.h @interface UINavigationBar(UINavigationBarCategory) UIImageView *backgroundView; -(void)setBackgroundImage:(UIImage*)image; -(void)insertSubview:(UIView *)view atIndex:(NSInteger)index; @end//CustomNavigationBar.m @implementation UINavigationBar(UINavigationBarCategory) -(void)setBackgroundImage:(UIImage*)image{ if(image == nil) { [backgroundView removeFromSuperview]; } else {backgroundView = [[UIImageView alloc] initWithImage:image];backgroundView.tag = 1; backgroundView.frame = CGRectMake(0.f, 0.f,self.frame.size.width, self.frame.size.height);backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight; [self addSubview:backgroundView];[self sendSubviewToBack:backgroundView]; [backgroundView release];} } //for other views - (void)insertSubview:(UIView *)viewatIndex:(NSInteger)index { [super insertSubview:viewatIndex:index]; [self sendSubviewToBack:backgroundView]; } @end//YourViewController.m - (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];[self.navigationController.navigationBarsetBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];}Objective-c代码 //CustomNavigationBar.h @interface UINavigationBar(UINavigationBarCategory) UIImageView *backgroundView; -(void)setBackgroundImage:(UIImage*)image; -(void)insertSubview:(UIView *)view atIndex:(NSInteger)index; @end//CustomNavigationBar.m @implementation UINavigationBar(UINavigationBarCategory) -(void)setBackgroundImage:(UIImage*)image{ if(image == nil) { [backgroundView removeFromSuperview]; } else {backgroundView = [[UIImageView alloc] initWithImage:image];backgroundView.tag = 1; backgroundView.frame = CGRectMake(0.f, 0.f,self.frame.size.width, self.frame.size.height);backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight; [self addSubview:backgroundView];[self sendSubviewToBack:backgroundView]; [backgroundView release];} } //for other views - (void)insertSubview:(UIView *)viewatIndex:(NSInteger)index { [super insertSubview:viewatIndex:index]; [self sendSubviewToBack:backgroundView]; } @end//YourViewController.m - (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];[self.navigationController.navigationBarsetBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];}

0 0