[英]Swift - Changing background color to transparent
我注意到我的一个按钮的背景颜色值 = 'default'
当按钮突出显示时,我想将不同的按钮更改为也具有“默认”背景颜色,这是我的代码:
@IBAction func buttonTouchUpInside(_ sender: UIButton) {
registerBtn.setTitleColor(UIColor.white, for: UIControlState.highlighted);
}
我该如何实现这一点,所以 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
}
使用UIColor.clear
使颜色透明。
苹果称其为“默认”而不是“清晰”,这非常令人困惑。
迅速
button.backgroundColor = UIColor.clear
客观-C
button.backgroundColor = [UIColor clearColor];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.