Ömer ASLAN Ömer ASLAN - 5 months ago 40
iOS Question

Swift: remove background color when button clicked

I created a button and when it is clicked, it's background image changes. However the issue is when I click it , it gives a rectangle with a color.


enter image description here

Should Be:

enter image description here

I don't want to see that clicking background.

My codes are below:

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

btn.setBackgroundImage(off, forState: .Normal)


When you use Storyboard buttons which is IBOutlet UIButton object. By default it is not set as a custom type. So if you set background of this button as an image, It shows border of that button with the image.

For Storyboard variables:

  1. Select UIButton control in Storyboard. Go to Attribute Inspector > Change type System to Custom

See the reference images

enter image description here

enter image description here

For programmatically created buttons:

If you create UIButton object programmatically than set button type as UIButtonTypeCustom.

Objective C Code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

Swift Code:

var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton