1. 程式人生 > >UINavigationBar的自定義圖片

UINavigationBar的自定義圖片

。h

#import <UIKit/UIKit.h>

@interface UINavigationBar (UINavigationBarCategory)

UIImageView *backgroundView;

- (void)setBackgroundImage:(UIImage*)image;
- (void)insertSubview:(UIView *)viewatIndex:(NSInteger)index;

@end

。m
#import "CustomNavigationBar.h"

@implementation UINavigationBar (UINavigationBarCategory)

-(void)setBackgroundImage:(UIImage*)image
{
    if(image ==nil)
    {
      [backgroundView removeFromSuperview];
    }
    else
    {
      backgroundView = [[UIImageView alloc]initWithImage:image];
      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
{
    [superinsertSubview:view atIndex:index];
    [selfsendSubviewToBack:backgroundView];
}

@end