更改导航栏每个导航上的背景图像

时间:2011-07-14 11:35:21

标签: iphone image background navigation

我正在开发iPhone应用程序。我添加了导航栏背景图像

使用界面: -

@interface UINavigationBar (backgroundImageWithTitle)

方法: -

- (void)drawRect:(CGRect)rect

通过此方法,导航栏背景图像被设置一次。

我想从不同的.m文件中调用它,以便在条形图上分配不同的图像。

如何实施?

提前致谢。

6 个答案:

答案 0 :(得分:8)

CustomNavigation.h

    #import <Foundation/Foundation.h>


    @interface UINavigationBar (UINavigationBarCustomDraw){

    }

    @end

CustomNavigation.m

    @implementation UINavigationBar (UINavigationBarCustomDraw)

    - (void) drawRect:(CGRect)rect {

        [self setTintColor:[UIColor colorWithRed:0.5f
                                           green: 0.5f
                                            blue:0 
                                           alpha:1]];

        if ([self.topItem.title length] > 0) {


            if ([self.topItem.title isEqualToString:@"First"]) {

                [[UIImage imageNamed:@"First.png"] drawInRect:rect];

            }

            else if ([self.topItem.title isEqualToString:@"Second"]) {

                [[UIImage imageNamed:@"Second.png"] drawInRect:rect];                   

            }




            CGRect frame = CGRectMake(0, 0, 320, 44);
            UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
            [label setBackgroundColor:[UIColor clearColor]];
            label.font = [UIFont boldSystemFontOfSize: 20.0];
            label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
            label.textAlignment = UITextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.text = self.topItem.title;
            self.topItem.titleView = label;





        } 


        else {
            [[UIImage imageNamed:@"wood.png"] drawInRect:rect];
            self.topItem.titleView = [[[UIView alloc] init] autorelease];
        }



    }

    @end

如果你想First.png在FirstViewController中设置navigationBar背景图像,那么

在你的FirstViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"First";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }

如果你想Second.png在SecondViewController中设置navigationBar背景图像,那么

在你的SecondViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"Second";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }

答案 1 :(得分:1)

在iOS 5上,这不起作用,但好消息是有一种简单的方法可以做到这一点

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];

注意:这仅适用于iOS 5及更高版本,因此如果您想向后兼容,请务必检查iOS版本。

答案 2 :(得分:1)

或其他选项,以便代码仅在iOS 5的情况下运行

if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];   

}

答案 3 :(得分:0)

我试过这个

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“name”] forBarMetrics:UIBarMetricsDefault];

适用于iOS 5.0但在iOS 4.3或更低版本中崩溃。

您已设置条件即

iOS 5

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“name”] forBarMetrics:UIBarMetricsDefault];

<强>否则

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“name”]];

但仍然存在显示图像下方的行的问题。

或者你可以这样做

self.navigationItem.hidesBackButton = YES; 
UINavigationBar *navBar = self.navigationController.navigationBar;

UIImage *image = [UIImage imageNamed:@"image.png"];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
imgView.image = image;
[tempView addSubview:imgView];
[navBar clearBackgroundImage];
[navBar addSubview:tempView];
[tempView release];

答案 4 :(得分:0)

你可以这样试试

if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
    //[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];   

    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
}

在其他情况下你可以添加以前的代码,即setBackgroundImage ....

答案 5 :(得分:0)

横向和< >肖像即可。

我做的是:

@implementation UINavigationBar (BackgroundImage)

- (void)drawRect:(CGRect)rect 
{
    UIImage *image;

    if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width.
    {
        image = [UIImage imageNamed: @"portraitNavigationBar.png"];
    }
    else
    {
        image = [UIImage imageNamed: @"landscapeNavigationBar.png"];
    }

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

@end

幸运的是我的应用程序支持旋转 - 所以,当旋转时 - 导航栏会自动重绘。

但是要手动重绘它 - 你也可以打电话:

[navigationController.navigationBar setNeedsDisplay];