我创建了一个按钮,单击它时,它的背景图像会发生变化。然而,问题是当我点击它时,它会给出一个带有颜色的矩形。
图片:
应该:
我不想看到那个点击背景。
我的代码如下:
let on = UIImage(named: "on") as UIImage
let off = UIImage(named: "off") as UIImage
btn.setBackgroundImage(off, forState: .Normal)
最佳答案
当您使用 Storyboard按钮时,它是 IBOutlet UIButton
对象。默认情况下,它未设置为自定义类型。因此,如果您将此按钮的背景设置为图像,它会显示该按钮的边框和图像。
对于 Storyboard 变量:
- 在 Storyboard 中选择
UIButton
控件。转到 Attribute Inspector> 将 System 类型更改为 Custom
查看引用图片
对于以编程方式创建的按钮:
如果您以编程方式创建 UIButton
对象,而不是将按钮类型设置为 UIButtonTypeCustom
。
objective-c 代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
快速代码:
var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
关于ios - Swift:单击按钮时删除背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271396/