Search code examples
swiftswiftuiuicolorxcode11nscolor

Can I use color literals in SwiftUI


I want to use my color assets from the assets catalog using color literals. In UIKit, we can directly use color literal is there any way to use color literals in SwiftUI

I have searched from Color initializer with UIColor parameter but none found


Solution

  • Note: From Xcode 11 Beta 5, Color has an initializer for this already and no need to implement it.

    So you can use colorLiteral like this:

    Color(#colorLiteral(red: 1, green: 0, blue: 0, alpha: 1))
    

    preview

    Or any other CGColor, UIColor (or NSColor for macOS):

    Color(.red)