我的 View 类(UIView
的后代)中有一些自定义外观属性。我想根据这些属性自定义 View 外观,但我不能在初始化程序中执行此操作,因为使用 [[MyClass appearance] setFoo:…]
设置的值在那一点:
@interface View : UIView
@property(strong) UIColor *someColor UI_APPEARANCE_SELECTOR;
@end
@implementation View
@synthesize someColor;
// Somewhere in other code before the initializer is called:
// [[View appearance] setSomeColor:[UIColor blackColor]];
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
NSLog(@"%@", someColor); // nil
return self;
}
@end
它们已经在 layoutSubviews
中设置,但这不是执行 View 自定义的好点,因为某些自定义可能会再次触发 layoutSubviews
,从而导致死循环。
那么,执行自定义的好点是什么?或者有没有办法触发应用外观值的代码?
最佳答案
一种可能的解决方法是直接从代理中获取值:
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
NSLog(@"%@", [[View appearance] someColor); // not nil
return self;
}
当然,这会扼杀根据 View 容器改变外观的选项,而且通常很难看。我发现的第二个选项是在 setter 中执行自定义:
- (void) setSomeColor: (UIColor*) newColor
{
someColor = newColor;
// do whatever is needed
}
我仍然希望有一些在设置外观属性后调用的钩子(Hook)。
关于ios - 我什么时候可以开始使用通过 UIAppearance 设置的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10737394/