简体   繁体   中英

How can I change the background image for my NavigationBar on a per page basis?

I've been looking around for a way to change the background image of my NavigationBar and control the appearance of my NavigationBar as the user navigates the app.

I understand that the accepted approach for changing the background image is :

@implementation UINavigationBar (UINavigationBarCategory)

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

@end

However, that changes the appearance of the NavigationBar throughout the whole app. How can I change the background image of the NavBar as the user navigates from one view to the next?

Thanks in advance for your help!

You need to set some state somewhere about the current page or currently appropriate image to use, probably in each of your viewWillAppear: methods. Then modify your drawRect: function above to reference that state.

To cause the bar to be redrawn, call [myNavigationBar setNeedsDisplay] when you update the state. This will cause drawRect to be invoked.

我真的建议你阅读Sebastian Celis的教程,这对我很有帮助 - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.

@implementation MyViewController {
    UIImage *_defaultImage;
}

- (void)viewWillAppear:(BOOL)animated {   
    _defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}

You can use this code any where in your application(Call the method in viewWillAppeare:) by calling the method to change the navigation bar image. If You call the method in didFinishLanch means the navigation bar image is set to the whole app.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [[UIDevice currentDevice] systemVersion];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9)
    {
        [self customizeAppearance];
    }

}

- (void)customizeAppearance
{
    UIImage *navbarimage = [[UIImage imageNamed:@"blckapplication_bar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0,0)];
    [[UINavigationBar appearance] setBackgroundImage:navbarimage forBarMetrics:UIBarMetricsDefault];

    // Create resizable images    
    // Set the background image for *all* UINavigationBars
}

是不是可以在导航栏上使用图像视图(添加)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM