Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes" enter image description here

It causing this underline in application (first picture with enabled setting, second without) enter image description here

Any idea how to programatically or in storyboard disable it?

I tried attributed text but I get same result :(

I'm newbie in Swift.

Thanks for help!

like image 339
user1085907 Avatar asked Dec 30 '15 18:12

user1085907


1 Answers

I totally agree with @maddy's comment: It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.

But I did stumble on a way to accomplish the task at hand...

In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.

Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.

see the Attributes inspector for UIButton here.

Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.

like image 139
Rudi Wever Avatar answered Sep 25 '22 22:09

Rudi Wever