我注意到我的一个按钮的背景颜色值为 'default'
当按钮突出显示时,我想更改一个不同的按钮以具有“默认”背景颜色,这是迄今为止我的代码:
@IBAction func buttonTouchUpInside(_ sender: UIButton) {
registerBtn.setTitleColor(UIColor.white, for: UIControlState.highlighted);
}
Run Code Online (Sandbox Code Playgroud)
我如何实现这一点,以便 registerBtn 具有基本上透明的背景颜色?
在评论中我发现:
(我将根据需要编辑上面的内容。我不完全确定的是当第一个按钮不再突出显示后会发生什么。)
如果是上述情况,则需要进行以下操作:
可能最简单的方法不是通过 IB,而是通过代码。在 IB 中添加按钮后,为它们创建 IBOutlets。然后你可以这样做:
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
firstButton.addTarget(self, action: #selector(highlightSecondButton), for: .touchDown)
firstButton.addTarget(self, action: #selector(highlightSecondButton), for: .touchDragEnter)
}
func highlightSecondButton(_ sender:UIButton) {
secondButton.setTitleColor(UIColor.white, for: UIControlState.normal)
secondButton.backgroundColor = nil
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11474 次 |
| 最近记录: |