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 UIBarButtonItem
s 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.
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?
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.
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With