ios - Swift:单击按钮时删除背景颜色

标签 ios user-interface swift

我创建了一个按钮,单击它时,它的背景图像会发生变化。然而,问题是当我点击它时,它会给出一个带有颜色的矩形。

图片:

enter image description here

应该:

enter image description here

我不想看到那个点击背景。

我的代码如下:

let on = UIImage(named: "on") as UIImage
let off = UIImage(named: "off") as UIImage

btn.setBackgroundImage(off, forState: .Normal)

最佳答案

当您使用 Storyboard按钮时,它是 IBOutlet UIButton 对象。默认情况下,它未设置为自定义类型。因此,如果您将此按钮的背景设置为图像,它会显示该按钮的边框和图像。

对于 Storyboard 变量:

  1. 在 Storyboard 中选择 UIButton 控件。转到 Attribute Inspector> 将 System 类型更改为 Custom

查看引用图片

enter image description here

enter image description here

对于以编程方式创建的按钮:

如果您以编程方式创建 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/

相关文章:

user-interface - 哪个 Windows 的 GUI 是最先进的?

user-interface - PyQt5 信号和插槽 'QObject has no attribute' 错误

ios - Swift - 位置提示不会很快发生

ios - 阻止 Mapkit 自定义 MKAnnotationView 在点击时消失

ios - 如何在单 View 应用程序中将 thirdviewcontroller 弹出到 firstviewcontroller

ios - 在iOS上设置默认 session 长度?

c# - Windows 窗体中的内容在最大化时移动 - C#

ios - 无法从自定义搜索栏修改高度 UITextField

android - 如何将字节从 Swift (iOS) 传递到 Kotlin 通用模块?

ios - CoreData 子上下文、NSFetchedResultsController 和主线程