Define a global color

ios, objective-c, uicolor

Solution

You should create a category, not a subclass. This will extend the UIColor class, and add your colors to it.

.h

#import <UIKit/UIKit.h>

@interface UIColor (CustomColors)

+ (UIColor *)myColorLightGreyBGColor;

@end

.m

#import "UIColor+CustomColors.h"

@implementation UIColor (CustomColors)



+ (UIColor *)myColorLightGreyBGColor {

    static UIColor *lightGreyBGColor;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lightGreyBGColor = [UIColor colorWithRed:241.0 / 255.0 
                                           green:241.0 / 255.0
                                            blue:241.0 / 255.0 
                                           alpha:1.0];
    });

    return lightGreyBGColor;
}

@end

By defining your colors this way, and #importing the category, you can apply this custom color the way you were already trying to.

Problem

I want to define a global colour that I can reuse for a downstate for various custom ui cells Not sure if this is the correct way to do this but.. I've defined a Class called lightGreyUIColor which has this .h file - ``` #import <UIKit/UIKit.h> @interface lightGreyUIColor : UIColor + (UIColor*)lightGreyBGColor; @end ``` and this . m file - ``` #import "lightGreyUIColor.h" @implementation lightGreyUIColor + (UIColor*)lightGreyBGColor { return [UIColor colorWithRed:241.0/255.0 green:241/255.0 blue:241/255.0 alpha:1]; } @end ``` I have included the lightGreyUIColor.h file in the implementation file for the tableview and tried to reference it as folows - ``` cell.backgroundColor = [UIColor lightGreyBGColor]; ``` Which just produces a no known class or method error for lightgreyBGColor, where am I going wrong and is there a better way to implement a global style than this?

Original source