Search code examples
swiftassetsuicolor

How to set alpha for an Assets Color in Swift


I just wanted to know how to change the opacity (alpha) of an asset color I have. When I try this UIColor(named: "something", alpha: 0.4), Xcode complains: Extra argument 'alpha' in call.

Is there any way I can modify the opacity of an asset color programmatically?


Solution

  • UIColor, as mentioned by Jasur S., has the withAlphaComponent(_:).

    It can be used with any UIColor objects to modify its alpha:

    let color = UIColor(named: "something")?.withAlphaComponent(0.5)
    

    Creating custom extensions to cover existing functionality is an arguable good.