I know that for elements of classes UIButton
and UIBarButtonItem
they automatically assume window.tintColor
as the main colour, which results of an immediate change in case I set a new tintColor to window at any time in the app.
I was wondering if there is any way to make UILabel
elements to follow the same pattern, where once created they automatically assumer its default colour as window.tintColor
and if changing window.tintColor
at any time within my app runtime would also result in changing the UILabel
tintColour automatically?
I hope that makes sense.
UILabels
are a subclass ofUIView
, so when you are running in iOS 7 they will have atintColor
property and will inherit that color from their parent view if their tint color is set tonil
(which is default).From Apple's Documentation:
However, you also ask "if changing window.tintColor at any time within my app runtime would also result in changing the UILabel tintColour automatically?" Apple advises you to not change the tint color when items are on screen:
I would guess this is because there is no guarentee that all the various UI elements will detect the tintColor change and update their visible views. However, the
UIView
documentation suggests a workaround if you want to updatetintColor
while yourUILables
are on screen:So just make sure to call
tintColorDidChange
on any views currently on screen whose tint color should update when thetintColor
of their parent view changes.But why don't your
UILabel
's update their color?So the above helps you set and update your various
tintColor
's, but you're not seeing any effect - why?Well that has to do with what Apple designed Tint to indicate. From the Human Interface Guidelines:
Apple got rid of borders and gradients around interactive elements and replaced them with color - specifically
tintColor
. The whole idea behindtintColor
is that things users can tap on get it, and things they can't tap on don't.UILabel
is not an interactive element - it is a text description - and so Apple will let you set atintColor
on it (as anyUIView
has atintColor
) but setting thattintColor
will not change how it is drawn.So what should you do? First, be aware that making more than just buttons take on the tint color could be a poor UI choice for your app - iOS 7 users and Apple app reviewers both will be expecting those rules to be followed.
So are you forced to keep your
UILabel
free from color then?No - especially if you do things "right". Apple explains:
I would suggest you consider the UI of your app. If you really want your non-intereactive elements to have the same
tintColor
as your interactive elements, then make sure you use something more, like a border or background color, so your users (and Apple app reviewers) know what is clickable and what is not.As to how you should update the colors, you can either manually set the
textColor
property to be whatever color you want, or you'll need to make your own subclass ofUILabel
that overrides- (void)tintColorDidChange
to update thetextColor
property when notifications are sent out - allowing you to have aUILabel
whose text updates to match thetintColor
of its parent.I hope this helps!
Found above explanation to be helpful - particularly the pointer to tintColorDidChange(). However, getting it to work out right didn't work at first, but finally came up with a code example that does. Basically, I had a tableView with cells containing images and labels. The images would update with a change in tintColor, but not the labels - which didn't look right. Better for either both to change or neither change. The code below is for the cell in the tableView. This was written with Swift 4.2.