我创建了一个自定义的 UIColor 类,这样我就可以轻松地更新整个应用程序的颜色。我有一个带有各种其他颜色设置的 UITableView。我不知道如何根据选择将自定义颜色类更新为我的新颜色。
感谢帮助
为清楚起见编辑:
自定义类:
+ (UIColor *)NPSBackgroundColor;
+ (UIColor *)NPSPrimaryColor;
+ (UIColor *)NPSSecondaryColor;
+ (UIColor *)NPSAccentColor;
+(UIColor *)NPSBackgroundColor{
return [UIColor colorWithRed: 0.909f green: 0.909f blue: 0.909f alpha:1];
}
+(UIColor *)NPSPrimaryColor{
return [UIColor colorWithRed: 0.255 green: 0.357 blue: 0.655 alpha: 1];
}
+(UIColor *)NPSSecondaryColor{
return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}
+(UIColor *)NPSAccentColor{
return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}
我想在用户点击按钮时更新说“primaryColor”....
最佳答案
您的代码是硬编码的,具有无法更改的特定颜色。您需要重构代码以返回无法修改的变量。像这样:
你的.h:
@interface UIColor (MyColors)
+ (UIColor *)NPSBackgroundColor;
+ (UIColor *)NPSPrimaryColor;
+ (UIColor *)NPSSecondaryColor;
+ (UIColor *)NPSAccentColor;
+ (void)setNPPrimaryColor:(UIColor *)color;
@end
你的.m
#import "UIColor+MyColors.h"
static UIColor *NPSBackgroundColor = nil;
static UIColor *NPSPrimaryColor = nil;
static UIColor *NPSSecondaryColor = nil;
static UIColor *NPSAccentColor = nil;
@implementation UIColor (MyColors)
+(UIColor *)NPSBackgroundColor{
if (!NPSBackgroundColor) {
NPSBackgroundColor = [UIColor colorWithRed: 0.909f green: 0.909f blue: 0.909f alpha:1];
}
return NPSBackgroundColor;
}
+(UIColor *)NPSPrimaryColor{
if (!NPSPrimaryColor) {
return [UIColor colorWithRed: 0.255 green: 0.357 blue: 0.655 alpha: 1];
}
return NPSPrimaryColor;
}
+(UIColor *)NPSSecondaryColor{
if (!NPSSecondaryColor) {
return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}
return NPSSecondaryColor;
}
+(UIColor *)NPSAccentColor{
if (!NPSAccentColor) {
return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}
return NPSAccentColor;
}
+ (void)setNPSPrimaryColor:(UIColor)color {
NPSPrimaryColor = color;
}
@end
请随意添加其他 setter 。
关于ios - 如何在 iOS 中更新自定义类别 (UIColor)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567142/