ios - 我什么时候可以开始使用通过 UIAppearance 设置的属性?

标签 ios cocoa-touch uiappearance

我的 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/

相关文章:

ios - 无法访问 NSManagedObject 子类的属性(给出 EXC_BAD_ACCESS)

iphone - 关于图像Alpha值的问题

iphone - 适用于 iOS 5/6 的在线实时多人游戏

ios - UINavigationBar 横向标题字体大小

ios - 使用 UIAppearance 代理样式化 UILabel

ios - 我什么时候应该停止更新位置管理器?

ios - 错误地集成 native 图表显示

ios - UICollectionReusableView 中的标签始终为零

iOS 编程 : Problem extending UIButton class

ios - 使用 UIAppearance 设置 UITableView 的背景颜色