简体   繁体   中英

How to Access Asset Catalog colour set programmatically

I want to use a colour set within my code. i want the asset file to be read so that it can access the colour set.

so i have set my name of the colour set to bottomNavigation.

I have tried using UIColor(named: "bottomNavigation") but it does not pick up the colour.

i know that you can reference an asset file by doing this for an image:

UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];

But i'm not sure how you would do that for a colour or how to reference the Asset file to get the colour set

I could not find an easier way, but can achieve it in the following manner

Press Cmd + Shift + L to open library and then select Color tab.

Then drag that color to the code. Something like this. 在此处输入图片说明

This is, assuming that you have added a color in your .xcassets And want to use that color.

If you want to use the color in xibs, it is easily available in the Named Colors section, if you tap on color.

You can achieve this with following easy 4 steps:

  1. Adding colors to an asset catalog : Go to Assets folder in your Project -> Tap on Plus button -> After selecting your .xcassets directory in Xcode, press the plus button on the bottom left and select “New Color Set”. Here's the attached link(GIF) it will help you about adding ColorSet.

  2. Using asset catalog colors in Storyboards : In order to user asset catalog colors in Storyboard or an Interface Builder file is pretty straightforward. Color fields, including background colors, text colors, should display the colors you defined in the asset catalog under the “Named Colors” section. Here's the attached link(GIF) it will help you about using ColorSet from Storyboard.

  3. Using asset catalog colors in CODE : Create an Extension first.

You first need to create an Extension of Custom font color(I had added code below).

extension UIColor {
class var appBG: UIColor {
    if let color = UIColor(named: "SillyBlue") {
        return color
    }
    fatalError("Could not find appBG color")
  }
}
  1. Then directly use in the code , example is given below:

    self.view.backgroundColor = UIColor.appBG

Let me know if you face any issue.

It should work.

UIColor(named:) requires iOS 11 and newer versions.

First of all, check the name of your asset.

颜色名称

After that, you should check the name in the code:

someView.backgroudColor = UIColor(named: "CustomColor")

UIColor(named: "name") is only available in iOS 11 and forward as you read the documentation for it:

@interface UIColor (UIColorNamedColors)
+ (nullable UIColor *)colorNamed:(NSString *)name NS_AVAILABLE_IOS(11_0);      // load from main bundle
+ (nullable UIColor *)colorNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection NS_AVAILABLE_IOS(11_0);
@end

In swift:

extension UIColor {

    @available(iOS 11.0, *)
    public /*not inherited*/ init?(named name: String) // load from main bundle

    @available(iOS 11.0, *)
    public /*not inherited*/ init?(named name: String, in bundle: Bundle?, compatibleWith traitCollection: UITraitCollection?)
}

So what i found is that a space was causing the issue with the colour not being referenced, UIColor (named:"bottomNavigation") . It might not give an error but there shouldn't be any space between UIColor and the bracket: UIColor(named:"bottomNavigation")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM