Search code examples
iosobjective-cuiappearance

UI_APPEARANCE_SELECTOR is not working


Specs: Xcode 7.0.1, iOS to Build 9.0, 8.4, Device iPhone

iam working on UI_APPEARANCE_SELECTOR and try to set the appearance for colors and fonts in a View. The View is added in a customClass for NavigationItem and has a title. I want that this title will be colorized and styled with a font. The Custom subclass from the Navigation Item is set in the Storyboard for the ViewControllers NavigationItem.

Here is my current state: AppDelegate.

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

The Header of my SubTitleView

@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

In my Class File i do the following when adding the titleLabel to the View:

- (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;
}

When i run my app the appearance will not be set. I have a black title and a black subtitle. I don't know what i'am doing wrong.


Solution

  • I found the solution now: When MySubtitleView is loaded the appearance is not set. Its set after the view is loaded, but then nothing will happen because i didnt care of that. Now i overrde the setter of the titleLabelTextColor and write the color again to the label. Here the appearance will set it and the setter will add this to the textField

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

    EDIT: I think this question was difficult, i couldnt find anything to fix it so i mark this Answer as correct answer and let it in. Think different :)