UI_APPEARANCE_SELECTOR 不工作

UI_APPEARANCE_SELECTOR is not working

规格:Xcode 7.0.1,iOS 至 Build 9.0、8.4,设备 iPhone

我正在处理 UI_APPEARANCE_SELECTOR 并尝试在视图中设置颜色和字体的外观。视图添加到 NavigationItem 的自定义 Class 中,并有一个标题。我希望这个标题可以用一种字体着色和设置样式。 Navigation Item 的自定义子类在 ViewControllers NavigationItem 的 Storyboard 中设置。

这是我目前的状态: AppDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    [[MySubtitleView appearance] setTitleLabelTextFont:[UIFont systemFontOfSize:14]];
    [[MySubtitleView appearance] setTitleLabelTextColor:[UIColor whiteColor]];
    ...
}

我的 SubTitleViewHeader

@import UIKit;

@interface MySubtitleView : UIView

@property (nonatomic) UILabel *titleLabel;
@property (nonatomic) UILabel *subtitleLabel;
@property UIColor *titleLabelTextColor UI_APPEARANCE_SELECTOR;
@property UIColor *subtitleLabelTextColor UI_APPEARANCE_SELECTOR;
@property UIFont *titleLabelTextFont UI_APPEARANCE_SELECTOR;
@property UIFont *subtitleLabelFont UI_APPEARANCE_SELECTOR;
@end

在我的 Class 文件中,我在将 titleLabel 添加到视图时执行以下操作:

- (UILabel *)titleLabel
{
    if (_titleLabel) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _titleLabel.textColor = self.titleLabelTextColor;
    _titleLabel.font = self.titleLabelTextFont;
    return _titleLabel;
}

当我 运行 我的应用程序外观不会被设置。我有一个黑色的标题和一个黑色的副标题。我不知道我做错了什么。

我现在找到了解决方案: 加载 MySubtitleView 时,外观未设置。它是在加载视图后设置的,但随后什么也不会发生,因为我不在乎。现在我覆盖了 titleLabelTextColor 的 setter 并将颜色再次写入标签。此处外观将对其进行设置,setter 会将其添加到文本字段

- (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor
{
    _titleLabelTextColor = titleLabelTextColor;
    self.titleLabel.textColor = titleLabelTextColor;
}

编辑: 我认为这个问题很难,我找不到任何解决方法,所以我将此答案标记为正确答案并输入。不同的想法:)