天天看點

修改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];