ios - 单击 IOS 后按钮标签颜色更改

标签 ios objective-c uibutton

我有一段代码创建了一个按钮,在正确的空间大小颜色等:

UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:@"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];

然后我在按下按钮时有一个触发器:

[learnmorebutton addTarget:self action:@selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];

然后我想改变按钮背景和文本颜色,所以:

sender.titleLabel.textColor = [UIColor redColor];
sender.backgroundColor = [UIColor whiteColor];

背景颜色改变但文字消失。就像它也变白了一样。

最佳答案

你需要通过

设置标签的颜色
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

按钮不接受标签上的颜色变化直接导致它的外观随着他的状态而变化。这就是为什么您需要使用特定的 UIButton API为此。

关于ios - 单击 IOS 后按钮标签颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682880/

相关文章:

ios - 禁用/启用 UIDatepicker

ios - NSMutableArray 中的 NSDictionary (iOS)

iphone - 我将如何扭转这个等式?

ios - uicollectionviewcell 中的 AVPlayerViewController - 取消触摸

ios - 设备旋转时 TableView Cell 中显示的两个 uibutton

ios - 如何将 UIButton 粘贴到 UIScrollView 的底部? (Chatto 框架)

iphone - iOS 7 后退按钮符号?

ios - 如何让 UIAlertView 暂停线程并等待用户响应,iOS 开发

ios - 如何在 UIViewController 中使用 TableView?

ios - 如何在聚焦时调整 UISearchController.searchBar 的大小问题?