简体   繁体   中英

How to change UINavigationController background color?

I'm able to change the backgroung image of UINavigationController by overriding drawRect: method:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {

    UIImage *img  = [UIImage imageNamed: @"navController.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.tintColor = [UIColor blueColor];
}

@end

the background is what I intended to be and the tintColor as well, but when trying to set a color that isn't existing in UIColor class it fails and shows strange color:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {

    UIImage *img  = [UIImage imageNamed: @"navController.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.tintColor = [UIColor colorWithRed:(26/255) green:(103/255) blue:(159/255)                      alpha:1];
}

@end

How can I force UINavigationBar to show the color I want?

Note: I'm only having a problem with navigation controller buttons color since the background itself is set to image.

You need to do this:

self.tintColor = [UIColor colorWithRed:(26.0f/255.0f) green:(103.0f/255.0f) blue:(159.0f/255.0f) alpha:1.0f];

Otherwise you're doing integer arithmetic and you'll end up with 0 for all of them probably. Use floating point arithmetic and you get the values you desire.

这对我有用

self.navigationController.navigationBar.backgroundColor= [UIColor colorWithRed:57.0/255.0 green:158.0/255 blue:209.0/255 alpha:1.0];

You can also use:

navigationController.view.backgroundColor =.yourColor

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