30

iOS7 でナビゲーション コントローラーの navigationBar をカスタマイズしようとしていますが、タイトルの色が変わりません。私は次のことをしています:

    [navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:46.0f/256.0f green:46.0f/256.0f blue:46.0f/256.0f alpha:1]];
    [navigationController.navigationBar setTranslucent:NO];
    [navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
    [self presentViewController:navigationController animated:YES completion:nil];

navigationBar の半透明がオフになり、暗くなりますが、タイトルも暗いままです。また、カスタム ラベルを作成してタイトル ビューとして設定しようとしましたが、うまくいきませんでした。

タイトルの色を変更するにはどうすればよいですか?

4

8 に答える 8

66

外観 API は進化を続けており、UITextAttributeTextColor は NSForgroundColorAttributeName に置き換えられました。

[navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

2 つのことを追加
します。最初にキーが来て、次にオブジェクトが来ます。

ナビゲーション コントローラーのタイトル属性をグローバルに変更する場合は、外観 API を使用します。

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];

その外観 API 呼び出しをアプリ デリゲートの didFinishLaunchingWithOptions メソッドに入れます。

更新: Swift に相当するものを投稿することもできます。

個々のView ControllerのnavigationBarを更新するには、次を使用できます。

self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

アプリ全体でナビゲーション バーの外観を変更するには、次を使用できます。

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
于 2014-02-18T00:54:50.073 に答える
12

私のために働いていません。UINavigation をモーダル ビュー コントローラーとして使用する。

どうやらタイトルの色は uiapplication level に設定する必要があるようです

編集:

最良の方法は、各 VC コンテキストでナビゲーション タイトルを変更することです。

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
于 2013-11-28T13:04:54.460 に答える
8
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
于 2013-10-16T00:10:51.680 に答える
4

迅速に次のことを行います。

self.navigationController.navigationBar.titleTextAttributes = [
    NSForegroundColorAttributeName : UIColor.redColor(),
    NSFontAttributeName : UIFont.systemFontOfSize(20)
]
于 2015-02-19T10:55:20.297 に答える
4

Swift + UIAppearance バージョン:

    UINavigationBar.appearance().barTintColor = .blackColor()
    UINavigationBar.appearance().barStyle = .Black
于 2015-05-18T14:33:52.733 に答える
3

OPのコードの問題は、次の行です。

[navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];

文字色属性の名前と値が混同されています。そのはず

[navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}];
于 2014-02-17T13:34:36.997 に答える