How to change background color of UINavigationController?

I can change the inverse of the UINavigationController by overriding the 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 planned and tintColor , but when trying to set a color that does not exist in the UIColor class, it fails and shows a 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 make the UINavigationBar show the color I want?

Note. I only have a problem with the color of the buttons of the navigation controller, since the background itself is set to the image.

+4
source share
2 answers

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 perform integer arithmetic, and you end up with 0 for all of them. Use floating point arithmetic and you will get the desired values.

+10
source

It works for me

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

Source: https://habr.com/ru/post/1396521/


All Articles