12

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,095
  • 13
  • 148
  • 192
Mitul Bhadeshiya
  • 1,250
  • 1
  • 12
  • 30

9 Answers9

16

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
  • 122,346
  • 22
  • 162
  • 258
  • 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
  • 3
    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
14

You should go to Settings > General > Accessibility and turn on/off Button Shapes and then revisit the app in question. You should only see underlines and shapes when this control is on, and this is to indicate what buttons that look like text are actually tappable for those with accessibility needs.

Abhinandan Pratap
  • 2,188
  • 1
  • 16
  • 37
8

Set background image to the button.

[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
user4291543
  • 89
  • 1
  • 1
7

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
  • 37,791
  • 11
  • 79
  • 130
Larme
  • 22,070
  • 5
  • 50
  • 76
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,401
  • 12
  • 12
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,046
  • 4
  • 40
  • 54
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,783
  • 2
  • 26
  • 33
1

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
  • 206
  • 1
  • 7
Paresh Navadiya
  • 37,791
  • 11
  • 79
  • 130
0

To remove the unwanted Blue colour layer from UIButton in simulator

go to Settings > General > Accessibility and turn off Button Shapes and then reRun the app it will remove the unwanted blue layer.

Abhi
  • 241
  • 4
  • 19