我正在使用 swift,在我的应用程序中有一个 UIButton,我想在按下和按下时更改背景颜色和边框颜色。
最佳答案
您可以只创建两个函数并将它们连接到适当的事件。像这样:
@IBAction func up(){
myButton.backgroundColor = UIColor.whiteColor()
}
@IBAction func down(){
myButton.backgroundColor = UIColor.blackColor()
}
现在只需在界面生成器中连接它们。函数 up() 连接到 Touch Up Inside 和 Touch Up Outside。函数 down() 来触地。
但是有一个更好的方法,使用你的按钮并在代码中为状态设置他的背景图像。
//this will show when button is released
myButton.setBackgroundImage(UIImage(named:"background_image_normal"), forState:UIControlState.Normal)
//this will show when button is pressed
myButton.setBackgroundImage(UIImage(named:"background_image_highlighted"), forState:UIControlState.Highlighted)
关于ios - OnTouchUp 和 OnTouchDown 上的 Swift UIButton 背景更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875055/