Objective C中定义的UIColor常数 [英] Objective C defining UIColor constants

查看:280
本文介绍了Objective C中定义的UIColor常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的主题,一些自定义的颜色的iPhone应用程序。因为这些颜色将固定我的UI,我想在一个类定义的颜色被列入(Constants.h和Constants.m)。我怎么做? (简单地定义他们不起作用,因为UIColors是可变的,并会导致错误 - Initalizer不是常量)。

I have a iPhone application with a few custom-defined colors for my theme. Since these colors will be fixed for my UI, I would like to define the colors in a class to be included (Constants.h and Constants.m). How do I do that? (Simply defining them does not work because UIColors are mutable, and would cause errors - Initalizer not constant).

/* Constants.h */
extern UIColor *test;

/* Constants.m */
UIColor *test = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

谢谢!

推荐答案

一个的UIColor不可变。我通常做这种颜色,字体和图像。你可以很容易地修改它使用singleton或有一个静态初始化。

A UIColor is not mutable. I usually do this with colors, fonts and images. You could easily modify it to use singletons or have a static initializer.

@interface UIColor (MyProject)

+(UIColor *) colorForSomePurpose;

@end

@implementation UIColor (MyProject)

+(UIColor *) colorForSomePurpose { return [UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]; }

@end

这篇关于Objective C中定义的UIColor常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆