13

Any one know how to remove the UIButton underline that appears because of Accessibility?

(I know it's because the user turned on "Button Shapes")

enter image description here

enter image description here

How can I remove that programmatically, or by setting some property in Xcode?

jscs
  • 63,694
  • 13
  • 151
  • 195
Mitul Bhadeshiya
  • 1,280
  • 1
  • 12
  • 32

7 Answers7

17

Let me get this straight. Apple added an accessibility feature that lets users mark buttons with underlines if they want to.

You want a way to defeat this feature, specifically designed to help people with handicaps use their devices, when the feature is something that the user has to ask for.

Why?

It is very likely not possible using standard buttons. If you did figure out a way to do it, Apple would likely reject your app because it defeats a system function meant to help the disabled.

So the answer is: Don't do that.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 6
    This is an outstanding reply to such a question but it's not a solution to the question. This should be a comment. – rmaddy Apr 23 '14 at 17:46
  • 5
    Because there may be cases where seeing underlined buttons is not acceptable even by a visually impaired person. eg: In a custom keyboard, having this setting on, underlines every button on the keyboard which looks bad. Also some people would have enabled button shapes because they like it this way. – Jeet Apr 23 '16 at 06:08
  • 1
    rmaddy, it **is** an answer. The answer is "No. You can't do that." – Duncan C May 19 '16 at 18:42
  • Hackish or not (okay, definitely hackish) but `UIButton` instances are sometimes a very handy tool for laying out text and images with flexible alignment and insets. In these cases (where, of course, `userInteractionEnabled` is permanently turned off) it's preferable to not have the underline. – villapossu May 24 '16 at 20:13
  • @villapossu, What? `UIButton`s are hard at laying out text and images! You can't easily place the image on the right, or the text beneath the button. – Iulian Onofrei Aug 17 '17 at 08:14
  • @IulianOnofrei right, they're only simple for placing the image to the left – sure there are more hacks for easily aligning a right-side or top image but as said, they're hacks. (Here's my favorite: https://stackoverflow.com/a/32174204/913299) But nowadays I prefer to be a good citizen and lay out my images and titles with `UIImageView`s and `UILabel`s (thanks `UIStackView` for simplifying that). – villapossu Aug 17 '17 at 09:35
  • @villapossu, I wanted to mention `UIStackView` too. – Iulian Onofrei Aug 17 '17 at 09:39
  • For example, if you write a calculator using frameless UIButtons on a pre-drawn background, the underlines look stupid. There is a valid reason to remove the underlines in many contexts. – w0mbat Sep 20 '22 at 17:10
10

Set background image to the button.

[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
user4291543
  • 109
  • 1
  • 2
8

Check below code :

NSMutableAttributedString *attrStr = [[yourBtnHere attributedTitleForState:UIControlStateNormal] mutableCopy];//or whatever the state you want
[attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length])
                            options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                         usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop)
{
    NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
    [mutableAttributes removeObjectForKey:NSUnderlineStyleAttributeName];
    [attrStr setAttributes:mutableAttributes range:range];
}];

• With the inspector/IB: Select your UIButton.
Show the Attributes Inspector.
The Textsettings should be in Attributed. Select the text, click on the fond item remove the Underlining setting it at none.
enter image description here

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
Larme
  • 24,190
  • 6
  • 51
  • 81
4

The "Button shapes" is a new accessibility option in iOS 7.1. If the user want to have this option activated, there is nothing you can do. This is the user's choice.

Emilie
  • 2,413
  • 13
  • 12
  • I agree that the accessibility setting should be respected for common use cases of UIButton, but there are many specialized uses of UIButton where the underline is not appropriate. For example a custom keyboard or a calculator. – w0mbat Sep 22 '22 at 17:33
3

If button is underlined due accessibility button shape option then you could set button title by using image but not by text. Simply create image where text will be drawn and set it to the button. In this case iOS can't recognize text there and won't insert underline.
You could consider it as hack but not as clear solution.

Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
2

You can't turn off this accessibility feature.

Make a custom UILabel or UIView with UITapGestureRecognizer if you really want to get rid of it.

Community
  • 1
  • 1
Laszlo
  • 2,803
  • 2
  • 28
  • 33
2

First get attribute string from button which has been set.

NSMutableAttributedString *attrStr = [[yourBtnHere  attributedTitleForState:UIControlStateNormal] mutableCopy];

Remove Attribute using removeAttribute like this :

[attrStr removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0,[attrStr length])];
[attrStr addAttribute: NSUnderlineStyleAttributeName value: [NSNumber numberWithInt:0] range: [attrStr length]];

Reset attribute using addAttribute like this:

UIColor *textBtncolor = [UIColor blackColor];
[attrStr addAttribute:NSForegroundColorAttributeName value:textBtncolor range:NSMakeRange(0, attrStr.length)];

Now set attribute string in your button

[yourBtnHere setAttributedTitle:[attrStr copy] forState:UIControlStateNormal];
Lee
  • 306
  • 1
  • 7
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132