修改UINavigationbar背景


IOS5下在ViewDidLoad中添加代码

 if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
    }

IOS5以前的版本实现会比较麻烦

方法一:主要技巧就是用视图的drawInRect:方法绘制

如下为创建了一个UINavigationBar Category

// 其实现代码如下
@implementation UINavigationBar (UINavigationBarCategory)



- (void)drawRect:(CGRect)rect {

    //颜色填充
     UIColor *color = [UIColor redColor];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColor(context,CGColorGetComponents([color CGColor]));

    CGContextFillRect(context,rect);

    self.tintColor = color;



    //图片填充
     UIColor *color = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];

    UIImage *img = [UIImage imageNamed:@"bg.png"];

    [img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];

    self.tintColor = color;

}

@end

自定义图片背景以下两句代码是关键:
UIImage *img = [UIImage imageNamed:@"bg.png"];
[img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
或者:
UIImage *img = [UIImage imageNamed:@"bg.png"];
CGPoint point = {0,0};
[img drawAtPoint:point];
或者:
//加入旋转坐标系代码

// Drawing code

UIImage *navBarImage = [UIImage imageNamed:@"LOGO_320×44.png"];

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0, self.frame.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

CGPoint center=self.center;

CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));

CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height), cgImage);

CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height), navBarImage.CGImage);

CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height), cgImage);
扩展UINavigationBar的drawRect方法的这种自定义方法会影响到工程项目中所有的导航条栏。
类似在iOS5.0中,由于UINavigationBar、UIToolBar和UITabBar的实现方式改变,而drawRect:方法不会被调用了,所以就不支持这种通过定义导航条类别的方式来自定义导航条了。除非在这类控件的子类中实现。

//子类可以调用drawRect:方法
@interface MyNavigationBar : UINavigationBar

@end

@implementation MyNavigationBar

- (void)drawRect:(CGRect)rect {

  [super drawRect:rect];

}

@end

方法二:定义UINavigationBar的一个static函数

 + (UINavigationBar *)createNavigationBarWithBackgroundImage:(UIImage *)backgroundImage title:(NSString *)title {

        UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];

        UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];

        [customNavigationBar addSubview:navigationBarBackgroundImageView];

        UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];

        [customNavigationBar pushNavigationItem:navigationTitle animated:NO];

        [navigationTitle release];

        [navigationBarBackgroundImageView release];

        return customNavigationBar;

 }

下面是在需要生成UINavgationBar 的地方添加的代码 *ViewController.m:

 self.navigationController.navigationBar.hidden = YES;

  UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg" ofType:@"png"]];

  UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];

  [self.view addSubview:customNavigationBar];
  UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 75.0, 30.0)];

  if (_backButtonImage) {

         [backButton setImage:_backButtonImage forState:UIControlStateNormal];

  }else {

        [backButton setImage:[UIImage imageNamed:@"btnback.png"] forState:UIControlStateNormal];

 }

[backButton addTarget:self action:@selector(backButtonCliked:) forControlEvents:UIControlEventTouchUpInside];

 UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
 customNavigationBar.topItem.leftBarButtonItem = backBarButton;
 
 [backButton release];
 [backBarButton release];
 UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 30)];
 UIBarButtonItem *addBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
 if (_isFromFavorites) {
        [addButton setImage:[UIImage imageNamed:@"btn-delete-0.png"] forState:UIControlStateNormal];

       [addButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
 }else {
         [addButton setImage:[UIImage imageNamed:@"btn_add.png"] forState:UIControlStateNormal];
         [addButton addTarget:self action:@selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
 }
 customNavigationBar.topItem.rightBarButtonItem = addBarButton;
 [addButton release];
 [addBarButton release];


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧阳映雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值