iOS - Globally change navigation bar title color using appearance?

前端 未结 6 1723
情歌与酒
情歌与酒 2021-02-07 00:56

This crashes the app:

[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

Is there a way to do

6条回答
  •  面向向阳花
    2021-02-07 01:49

    Using modern syntax and code that actually runs, this is how to globally style your UINavigationBar title text:

    NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init];
    navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5
                                                             alpha:0.5];
    navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0);
    [[UINavigationBar appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor],
                                                            NSFontAttributeName : [UIFont fontWithName:@"Arial-BoldMT"
                                                                                                  size:30.0],
                                                            NSShadowAttributeName : navigationBarTitleShadow }];
    

    Note: NSShadow's shadowBlurRadius property is not respected.

    Note: Shadows are so iOS 6. Don't ever use them.

提交回复
热议问题