Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem tint color not working

I have recently noticed that the behaviour of the tintColor property has changed significantly in one of the recent iOS 10 updates.

I had a code which was working perfectly previously and which now is not working at all.

In one of my apps I have a collection of UIBarButtonItems which are added to the toolbarItems array of the view controller. They are displayed fine.

However, at some point I want to change the tint color of certain items in response to the user action. The code is trivial:

flagButton.tintColor = [UIColor redColor];

This code used to work before, but now it doesn't work! I have verified that a reference to the UIBarButtonItem exists and that this line of code is executed. However, the tint color is not changing as it should.

In the description of the tintColor property it says:

To refresh subview rendering when this property changes, override the tintColorDidChange() method.

I tried adding the tintColorDidChange method but it is not called. My assumption at this stage is that once tintColor is changed my button is not refreshed automatically.

Could someone help with finding a way to refresh it manually? The button is a standard UIBarButtonItem with an image.

Update 1

Actually I noticed that if I change the tintColor of the toolbar instead of an individual item the code is working. It seems that for some reason setting the tint color on individual items no longer works. Does anyone know why this is the case?

Update 2

Thanks to matt's hint I was able to find the source of the problem. It was caused by the code executed when the app was launched:

[[UITableViewCell appearance] setTintColor:[ColourTheme sharedTheme].navBackground];

Now, I am completely puzzled as to why this line caused problems with the UIBarButtonItem tint color... It might be because it introduced some ambiguities into the UIAppearance. Anyway, as I no longer need this line myself I can remove it easily. I will still leave my question open as I think that such behaviour is super weird and so, in a way, the question is still unanswered.

Just as an additional info - the view controller which experienced these problems contained a table view in it.

like image 929
Andriy Gordiychuk Avatar asked Jan 19 '17 14:01

Andriy Gordiychuk


3 Answers

I ran into the same issue and ended up solving it by taking the following steps:

1) Create an outlet to the bar button item. Mine was called "closeButton" 2) Write the following code:

override func viewWillAppear(animated: Bool) {
  let barButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
  closeButton.setTitleTextAttributes(barButtonAttributes, for: .normal)
}

Be sure to place the code in viewWillAppear as this overrides UIAppearance's default settings.

like image 121
Eman Harout Avatar answered Nov 14 '22 22:11

Eman Harout


I can't reproduce any problem. Here you can see we are alternating the tint color of the right bar button item between red and green; this is done by setting the bar button item's tintColor directly in code:

enter image description here

        let c = self.replyButton.tintColor
        if c == UIColor.red {
            self.replyButton.tintColor = .green
        } else {
            self.replyButton.tintColor = .red
        }

Therefore I conclude that the issue you are seeing is caused by something else you haven't told us about.

like image 38
matt Avatar answered Nov 14 '22 23:11

matt


So in my case, this line

UIActivityIndicatorView.appearance.tintColor = FlatWhite;

set in didFinishLaunchingWithOptions changed all UIBarItemItems.tintColor to default blue and it was not possible to change it other way than by setting tint color on parent bar...

When I comment this line everything work when uncomment it stops working. For me this is clear bug in IOS framework. That is hard to predict because it's little different in other cases.

like image 31
Renetik Avatar answered Nov 14 '22 23:11

Renetik