22

これにより、アプリがクラッシュします。

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

外観を使用してこれを行う方法はありますか?

4

7 に答える 7

66

これはうまくいきました:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
于 2012-06-12T21:21:11.570 に答える
4

Swift でこれを行う方法の例を次に示します。

UINavigationBar.appearance().titleTextAttributes =
  [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,
  NSForegroundColorAttributeName:UIColor.whiteColor()]
于 2015-09-15T11:38:57.853 に答える
3

UINavigationBar にタイトルまたは状態がない前に、アプリがクラッシュします...これらは UIButton メソッドです

あなたが必要

[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];
于 2012-06-12T20:55:57.370 に答える
2

@RyJの回答は素晴らしく、私にとってはうまくいきました。これについては、Ray Wenderlich のサイトに次のようなタイトルの優れたチュートリアルがあることを知っておいてください。

iOS 6 でのユーザー インターフェイスのカスタマイズ

セクションUINavigationBar のカスタマイズを参照してください。

グローバルに変更するナビゲーション バーのタイトルのコード スニペットを次に示します。

// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
  UITextAttributeTextColor,
  [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
  UITextAttributeTextShadowColor,
  [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
  UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"Arial-Bold" size:0.0],
  UITextAttributeFont,
  nil]];

もう 1 つのマイナーな点は、タイトル バーに既定の影があるように見えることです。そのため、属性を削除するだけでは影を取り除くことができません。代わりに、シャドウ オフセットを設定する必要があります。

UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
于 2013-07-18T08:18:53.753 に答える
0

次のコードを使用して、タイトル バーの色を変更しました。

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 0);

NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                            NSShadowAttributeName:shadow};

[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];
于 2015-06-27T20:20:01.563 に答える
0

実際に実行される最新の構文とコードを使用して、UINavigationBarタイトル テキストをグローバルにスタイル設定する方法は次のとおりです。

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

注:NSShadowshadowBlurRadiusプロパティは考慮されません。

注: 影は iOS 6 用です。使用しないでください。

于 2015-09-18T23:14:40.293 に答える