Swift UIButton - How to remove underline?

不想你离开。 提交于 2021-01-27 02:21:31

问题


I have problem when in iOS settings is enabled this setting "Button Shapes"

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

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!


回答1:


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.




回答2:


This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7

[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];

I found this answer works with SWFrameButton

And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...




回答3:


You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.




回答4:


Are you sure you want to do that? Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.




回答5:


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.



来源:https://stackoverflow.com/questions/34535241/swift-uibutton-how-to-remove-underline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!