Ömer ASLAN
Ömer ASLAN

Reputation: 139

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.

Image:

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)

Upvotes: 2

Views: 5319

Answers (2)

Mark Anthony Co
Mark Anthony Co

Reputation: 41

just use this

btn.setBackgroundImage(nil, forState: .Normal)

Upvotes: 3

Mehul Patel
Mehul Patel

Reputation: 23053

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

Upvotes: 3

Related Questions