Really basic question that I can't find the answer to. What is tint in Xcode and why is the default tint for all views blue? What does it affect? I've noticed that colors of some images within these views don't look quite right unless I set the tint to white and it's really annoying to have to set every new view's tint to white just because I don't understand what it does.
tintColor can be set for any individual view to color just one view, for the whole view in your view controller to color all its subviews, or even for the whole window in your application so that all views and subviews are tinted at once.
Tinted glass is not just an ideal form of protection for furnishings, it is also a great safeguarding tool for your health. If exposed to UV radiation whilst sitting by a window, this type of glass reduces the amount ultraviolet rays by 99%.
In color theory, a tint is a mixture of a color with white, which increases lightness, while a shade is a mixture with black, which increases darkness. Both processes affect the resulting color mixture's relative saturation. A tone is produced either by mixing a color with gray, or by both tinting and shading.
tintColor is a variable in UIView that returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself). If no non-default value is found, a system-defined color is returned (the shiny blue you always see).
Swift 5
Tint colour can be thought of as the colour to be used with images that have no colour of their own. Images have no colour of their own when they are rendered as a "template", as you can see from the rendering mode. This is because when an image is rendered as a template, their colour values are ignored and their alpha is set to 0 and is substituted with the tint colour.
For example, let's say you set a system image to a button:
let button = UIButton(frame: CGRect(origin: CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY), size: .init(width: 200, height: 200)))
let image = UIImage(systemName: "trash")
button.setImage(image, for: .normal)
self.view.addSubview(button)
All symbols image are template images which means their colour is determined by the tint colour you set. It so happens that the default tint colour is blue, which is why the symbol shows up as blue:
Tint colour is inherited to all the subviews, which means if you were to change the tint colour of one of the superviews to red:
self.view.tintColor = .red
Let's say you can set the alpha of this symbol to 1, which replaces the tint colour with its original colour, or force the symbol to be rendered with .alwaysOriginal
by using withRenderingMode(_:):
let image = UIImage(systemName: "trash")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
As I mentioned, the tint colours are inherited, which means you don't have to set every view's tint colour. You can either change UIWindow
's tint colour or change the global tint in the File Inspector of your Interface Builder:
Finally, if you want to get one of the custom images from your asset catalogue to be rendered as a template, you can set the rendering mode in the Attribute Inspector of the asset catalogue:
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