Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the tint colour of elements in UINavigationbar (iOS 11)

I have been using this code to change the tint color of element in the navigation bar:

UINavigationBar.appearance().tintColor = theme.labelColor

However, this is no longer working in iOS 11. Before iOS 11, buttons in the Navigation Bar were UINavigationButtons, but in iOS 11 they've been changed to _UIModernBarButton. I was able to change their tint color with UIButton.appearance().tintcolor, but that changes every button's.

Here's a comparison:

iOS 10 Vs. 11 UINavigationBar button Anyone has any idea how to change the button tint color in the navigation bar?

UPDATE 01/09/2017: Looks like _UIButtonBarButton has the correct tint color, but _UIModernBarButton overwrites it with the color set for UIButton.

UPDATE 18/09/2017:

"Engineering has provided the following feedback regarding this issue:

UIView.tintColor is not an appearance selector, and specifically is documented as not working correctly as an appearance property due to its inheritance properties."

like image 774
Dan Avatar asked Aug 26 '17 08:08

Dan


People also ask

How do I change the color of my UINavigationBar?

Open the project's storyboard file. Select the UINavigationBar from your UINavigationController scene. In the Attributes Inspector pane turn on these Appearances: “Standard”, “Compact”, “Scroll Edge”, and “Compact Scroll Edge”. For all four appearances, set the “Background” to “System Red Color”, for example.

What is tint color in iOS?

A color used to tint template images in the view hierarchy.


1 Answers

So I did find kind of a solution. I am setting the tint color via the appearance proxy for UIButton but only when contained in an UINavigationBar. This looks like it is working for me. However still hoping that this behaviour will change in the iOS 11 GM or someone can come up with a better solution. Here is my working code:

 if([UIButton respondsToSelector:@selector(appearanceWhenContainedInInstancesOfClasses:)]) {
    [[UIButton appearanceWhenContainedInInstancesOfClasses:@[UINavigationBar.class]]setTintColor:navTintColor];
}

Swift version of appearance proxy call:

UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = UIColor.red
like image 176
dehlen Avatar answered Sep 21 '22 19:09

dehlen