我使用以下代码创建了UIButton
:
applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:@"MyTitle" forState:UIControlStateNormal];
applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:@selector(saveSettingsToNSUser:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];
按钮最初显示为浅灰色的“MyTitle”。 但是当我按下它时标题颜色变为白色并且调用“saveSettingsToNSUser”函数。函数“saveSettingsToNSUser”中的NOTHING会影响按钮。我不明白发生了什么。我错过了什么吗?
答案 0 :(得分:3)
您应该使用setTitleColor:forState:
设置标题标签颜色。
[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];
答案 1 :(得分:0)
applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
//set title
[applyButton setTitle:@"MyTitle" forState:UIControlStateHighlighted];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:@"MyTitle" forState:UIControlStateNormal];
applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:@selector(saveSettingsToNSUser:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];