Swift:点击按钮时删除背景颜色
问题描述:
我创建了一个按钮,当它被点击时,它是背景图片的变化。然而问题是当我点击它时,它会给出一个带有颜色的矩形。Swift:点击按钮时删除背景颜色
图片:
应该是:
我不希望看到,点击背景。
我的代码如下所示:
let on = UIImage(named: "on") as UIImage
let off = UIImage(named: "off") as UIImage
btn.setBackgroundImage(off, forState: .Normal)
答
当您使用情节提要按钮,这是IBOutlet UIButton
对象。默认情况下,它不被设置为自定义类型。因此,如果您将此按钮的背景设置为图像,则会显示该按钮与图像的边框。
对于故事板的变量:
- 选择
UIButton
控制在故事板。转到属性检查器>更改类型系统到定制
见参考图像
对于编程的方式创建按钮:
如果以编程方式创建UIButton
对象,而不是将按钮类型设置为UIButtonTypeCustom
。
目标C代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Swift代码:
var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
答
只是用这个
btn.setBackgroundImage(nil, forState: .Normal)
检查按钮类型的自定义或没有。 – Kampai 2014-10-09 06:13:51
谢谢!有用。最好。 – 2014-10-09 06:16:19