3

これが私のコードです。押されると紫に変わる白いボタンを作成します。私が欲しいのは紫色のボタンです。

    if (!backButton) {
    backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:17];
    CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:23]];
    backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
    backButton.tintColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
    [backButton addTarget:self action:NSSelectorFromString(@"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:backButton];

私は何が欠けていますか?

4

2 に答える 2

2

残念ながら、tintColor プロパティは RoundedRect ボタン タイプでは機能しません。

ここを参照してください: UIButton.tintColor が機能しないのはなぜですか?

これを回避するために、色付けしたい UIButton の代わりに、単一セグメントの UISementedControl を使用します。

この回答はここでテクニックを説明しています: https://stackoverflow.com/a/4971371/785411

別の方法として、ボタンに紫色の画像を使用することもできますが、それにはもう少し作業が必要です。

于 2013-01-21T00:00:37.873 に答える
1

角丸長方形のボタンは変更が難しいため、代わりにカスタム ボタンを使用し、背景色を設定します。

if (!backButton) {
        backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setTitle:@"Back" forState:UIControlStateNormal];
        backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:17];
        CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:23]];
        backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
        backButton.layer.cornerRadius = 6.0f;
        backButton.backgroundColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
        [backButton addTarget:self action:NSSelectorFromString(@"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:backButton];

QuartzCore フレームワークを追加してインポートする必要があります (レイヤーのプロパティが機能するため)。

于 2013-01-21T00:30:50.340 に答える