Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:单击按钮时删除背景色_Ios_User Interface_Swift - Fatal编程技术网

Ios Swift:单击按钮时删除背景色

Ios Swift:单击按钮时删除背景色,ios,user-interface,swift,Ios,User Interface,Swift,我创建了一个按钮,当它被点击时,它的背景图像会改变。然而问题是当我点击它时,它会给出一个带有颜色的矩形 图片: 应该是: 我不想看到那种背景 我的代码如下: let on = UIImage(named: "on") as UIImage let off = UIImage(named: "off") as UIImage btn.setBackgroundImage(off, forState: .Normal) 当您使用情节提要按钮时,它是ibuibutton对象。默认情况下,它不会

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

图片:

应该是:

我不想看到那种背景

我的代码如下:

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

btn.setBackgroundImage(off, forState: .Normal)

当您使用情节提要按钮时,它是
ibuibutton
对象。默认情况下,它不会设置为自定义类型。所以,如果您将这个按钮的背景设置为图像,它将显示该按钮和图像的边框

对于情节提要变量:

  • 在情节提要中选择
    ui按钮
    控件。转到属性检查器将类型更改为自定义
  • 请参见参考图像

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

    如果以编程方式创建
    UIButton
    对象,则将按钮类型设置为
    uibuttonypecustom

    目标C代码:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    Swift代码:

    var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    
    就用这个

    btn.setBackgroundImage(nil, forState: .Normal)
    

    检查按钮类型是否为自定义。谢谢!它起作用了。最好的。