0

カスタム イメージと青いテキストを含む UIBarButtonItem が必要です。最初にテキスト、フォントの色、および画像を使用して UIButton を作成し、次に UIBarButtonItem のカスタム ビューを設定してこれを作成しました。

問題は、UIBarButtonItem を無効にすると、テキストが白に戻ることです。理由がわかりません。

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundImage:[UIImage imageNamed:@"images/ButtonHeader"] forState:UIControlStateNormal];
[customButton setTitle:myDynamicText forState:UIControlStateNormal];
[customButton setFrame:CGRectMake(0.0f, 0.0f, 70.0f, 44.0f)];
[customButton addTarget:myTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
[customButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12.0f]];
[customButton.titleLabel setTextColor:[UIColor blueColor]];

UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];

self.navigationBar.rightBarButtonItem = customBarButtonItem;
self.navigationBar.rightBarButtonItem.enabled = NO;

私が本当にやりたいのは、有効な状態と無効な状態でテキストの色を変えることですが、有効= NOのときにテキストが白くなる理由とそれを止める方法を誰かが教えてくれれば、その部分を自分で理解できます。

4

1 に答える 1

3

そのようなラベルを取得してフォントの色を設定するべきではありません。ボタンで設定する必要があります

[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

UIControlStateNormalはデフォルト値を設定するように機能しますが、その後、すべての異なる状態の色を変更できます。これらの状態のリストは、UIControlクラスリファレンスの下部にあります。Control State

于 2012-09-12T13:36:54.233 に答える