2010-11-23 100 views
2

我想使用自己的圖像更改導航欄背景,但我真的不知道該怎麼辦。我在谷歌搜索它,但一些樣本告訴導航欄模板。我的應用程序是一個分割視圖基礎。我怎麼能這樣做?使用我自己的圖像更改導航欄背景

回答

4

如果你想改變導航欄的背景太不使用類別,這可能是有幫助的,得到的感覺,看它是否符合你的需要:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

     UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)]; 
     //here for v, width= navBar width and height=navBar height 

    [v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]]; 

    [self.view addSubview:v]; 

    [v release]; 

    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]]; 

    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent]; 
} 
0

要將背景圖像添加到UINavigationBar,您需要創建一個擴展UINavigationBar的類別。只需找到下面給出的代碼並將其添加到您的實現文件中即可。

@implementation UINavigationBar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
UIImage *image = [UIImage imageNamed: @"background.png"]; 
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

執行後..你可以在你的應用程序的任何地方調用它,你可以看到每個視圖的變化。不要忘記將圖像添加到資源文件夾中。

+0

圖像沒有,類別表示擴展已經存在的類.... – marko 2010-11-23 09:56:49

+0

你可以在這裏找到更多的信息.​​.. http://www.youtube.com/watch?v=YHwnY-TOBlY – marko 2010-11-23 09:57:51

+0

你可以在@implementation – marko 2010-11-23 10:34:58

2
#import "ImageViewController.h" 

@implementation UINavigationBar (CustomImage) 

- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed:@"background.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 

@end 
implement this code in your .m file 


@implementation ImageViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.navigationController.navigationBar.tintColor = [UIColor blackColor]; 
    UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; 
    [self.navigationController.navigationBar insertSubview:backGroundView atIndex:0]; 
    [backGroundView release]; 
} 

@end 
3

您可以添加直接在導航欄的背景與下面的代碼

[navigation_bar setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forBarMetrics:UIBarMetricsDefault];