ios - UIButton 触摸时不显示突出显示

标签 ios objective-c uibutton

我正在尝试使用自定义的浅灰色 UIColor 突出显示我的 UIButton,并使文本变为白色。但是,我不确定该怎么做;这就是我在创建 UIButton 方面取得的进展。

cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelButton.frame = CGRectMake(10.0, calculatorView.frame.size.height-55, 100.0, 45.0);
    [cancelButton addTarget:self action:@selector(calculateMethod:)forControlEvents:UIControlEventTouchDown];
    [[cancelButton titleLabel] setFont:[UIFont fontWithName: @"Super-text" size:20.0]];
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];

最佳答案

要使白色文本突出显示,只需执行以下操作:

[cancelButton setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateHighlighted];

要更改背景,您需要执行 setBackgroundImage:forState: 并使用具有图案颜色的 UIImage,或者子类化 UIButton 并在 setHighlight: 方法中设置适当的背景颜色。

编辑:Swift 2.x 版本

cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)

编辑:Swift 3.0 版本

cancelButton.setTitleColor(.white, for: .highlighted)

关于ios - UIButton 触摸时不显示突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21133856/

相关文章:

java - 从字节数组创建列表

ios - Font Awesome 图标不适用于 UIButton IBOutlet

ios - 播放 MOV 文件的最佳方式是什么?

ios - 使用**压缩**顶点结构是否有任何速度损失?

ios - 如何在不崩溃的情况下重命名我的 .xcodeproj?

ios - 隐藏/显示 UI 元素与添加/删除 (Swift)

iphone - iOS 5 在它被解雇之前持有一个方法一段时间

objective-c - 带背景的 NSView 图像

ios - 按下按钮时更改文本颜色?

ios - titleLable.text 不工作