`

iOS UINavigationBar自定义背景

    博客分类:
  • ios
阅读更多

转自:http://blog.csdn.net/jinglijun/article/details/7259729

//CustomNavigationBar.h   

#import <UIKit/UIKit.h>

 

@interface UINavigationBar (UINavigationBarCategory)

 

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

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

@end  

 

//CustomNavigationBar.m  

#import "CustomNavigationBar.h"

 

 

@implementation UINavigationBar (UINavigationBarCategory)   

UIImageView *backgroundView; 

 

-(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 *)view atIndex:(NSInteger)index   

{   

    [super insertSubview:view atIndex:index];   

    [self sendSubviewToBack:backgroundView];   

}   

@end   



//YourViewController.m   

#import "CustomNavigationBar.h"

 

 

 

    //判断设备的版本

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000  

    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){  

        //ios5 新特性  

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"titleBar.png"] forBarMetrics:UIBarMetricsDefault];  

    }  

#endif 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics