ios - 如何在 iOS 上更改 UIButton 的文本颜色并使其保持选中状态

标签 ios swift uibutton uicontrol textcolor

我有一个非常小的 UIButton(无边框,透明背景上的白色文本颜色,其 superView 有深色背景)。当用户点击 UIButton 时,我会更改白色文本颜色的 Alpha channel ,使其呈现灰色外观,并向用户发出实际按下它的信号。现在,如何在按下按钮后使按钮保持选定状态?

textColor 几乎立即从灰色切换回白色,我希望颜色保持灰色,几乎表现得“粘性”,以便用户真正看到它被选中。我该如何实现这一目标?这是我到目前为止的代码。

logInButton.setTitleColor(pwStyle.appThemeTextFontColor, for: .normal)
        logInButton.setTitleColor(pwStyle.appThemeTextFontColor.withAlphaComponent(0.3), for: .highlighted)
        logInButton.setTitleColor(pwStyle.appThemeTextFontColor.withAlphaComponent(0.3), for: .selected)

^^ 上面的 pwStyle.appThemeTextFontColor 仅返回 UIColor.white

最佳答案

您可以将“选定”颜色添加到“禁用”按钮状态:

logInButton.setTitleColor(pwStyle.appThemeTextFontColor.withAlphaComponent(0.3), for: .disabled)

只需在按下时禁用该按钮..并在稍后重新启用它。

关于ios - 如何在 iOS 上更改 UIButton 的文本颜色并使其保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603932/

相关文章:

ios: UITableView reloadData 在滚动之前不工作

ios - 无法从 NSMutableArray 中正确检索对象

ios - 动画 UIButton 到全屏

ios - Swift UIButton 扩展动画覆盖

iphone - 检查 subview 中的点是否在父 View 中

ios - 以编程方式创建 UITabBarController,仅显示一个选项卡

swift - 使用导航栏在 Swift 中的 ViewController 之间存储数据

swift - 声明这些变量的合适位置在哪里?

uitableview - 在 tableView 单元格中添加按钮

iphone - UIButton 的自定义字体标题