Swift 中的图片点击按钮问题
Image clicked button issue in Swift
实际上如何确保我的按钮在用户单击时更改背景图像?
我的代码
btn1.setImage(UIImage(named:"dialpad1.png"),forState:UIControlState.Normal)
btn1.setImage(UIImage(named:"dialpad2.png"),forState:UIControlState.Highlighted)
我的背景图片
dialpad1.png
dialpad2.png
我的设置
我的屏幕
尝试在您的 UIButton 中使用 "Custom" 类型而不是 "System"。
您可以从 IB 更改它:
Register your button for touchdown
myButton.addTarget(self, action: "buttonTouchDownAction:", forControlEvents: UIControlEvents.TouchDown)
Register your button for touchupinside
myButton.addTarget(self, action: "buttonTouchUpInsideAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTouchDownAction(sender:UIButton!)
{
//set highlighted image
}
func buttonTouchUpInsideAction(sender:UIButton!)
{
//set unhighlighted image
}
感谢Midhun 议员,您的回答是最正确的。我已经按照你的建议去做了。我将 State Config 更改为 Highlighted 和 select image in Highlight Background Image。完全没有编码..
实际上如何确保我的按钮在用户单击时更改背景图像?
我的代码
btn1.setImage(UIImage(named:"dialpad1.png"),forState:UIControlState.Normal)
btn1.setImage(UIImage(named:"dialpad2.png"),forState:UIControlState.Highlighted)
我的背景图片
dialpad1.png
dialpad2.png
我的设置
我的屏幕
尝试在您的 UIButton 中使用 "Custom" 类型而不是 "System"。
您可以从 IB 更改它:
Register your button for touchdown
myButton.addTarget(self, action: "buttonTouchDownAction:", forControlEvents: UIControlEvents.TouchDown)
Register your button for touchupinside
myButton.addTarget(self, action: "buttonTouchUpInsideAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTouchDownAction(sender:UIButton!)
{
//set highlighted image
}
func buttonTouchUpInsideAction(sender:UIButton!)
{
//set unhighlighted image
}
感谢Midhun 议员,您的回答是最正确的。我已经按照你的建议去做了。我将 State Config 更改为 Highlighted 和 select image in Highlight Background Image。完全没有编码..