Ios UIButton仅在按下时显示tintColor,而不是在正常状态下

Ios UIButton仅在按下时显示tintColor,而不是在正常状态下,ios,objective-c,colors,Ios,Objective C,Colors,这是我的密码。它会创建一个白色按钮,按下按钮时会变成紫色。我想要的是一个紫色的纽扣 if (!backButton) { backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [backButton setTitle:@"Back" forState:UIControlStateNormal]; backButton.titleLabel.font = [UIFont fontWithNam

这是我的密码。它会创建一个白色按钮,按下按钮时会变成紫色。我想要的是一个紫色的纽扣

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

我缺少什么?

遗憾的是,tintColor属性不适用于RoundedRect按钮类型

请看这里:

为了解决这个问题,我使用了一个单段UISementedControl来代替我想要的ui按钮

这里的回答描述了该技术:


另一种选择是为按钮使用紫色图像-尽管这需要更多的工作。

圆形矩形按钮很难更改,因此请使用自定义按钮,并设置背景颜色:

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框架,并将其导入(以使图层属性正常工作)。

您没有使用
@selector(displaySettingsScreen)
的原因是什么?如果你这样做,你会得到编译时的安全检查。@ZevEisenberg,没有理由,我只是没有想到这一点。