16

I want to disable my UIButton, so I call:

button.enabled = FALSE;

However this makes the button see-through and I can see the elements underneath it. I don't mind that it changes colour, I just don't want it to be see-through.

I've tried ticking the boxes for 'opaque' and 'clip subviews' in IB, but no joy.

Is there an easy way to fix this? I suppose I could put a UIImageView with the same button image behind it, but it's hardly an elegant solution.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Smikey
  • 8,106
  • 3
  • 46
  • 74

3 Answers3

35

You can also do:

button.adjustsImageWhenDisabled = NO;
Snowman
  • 31,411
  • 46
  • 180
  • 303
27

Try userInteractionEnabled property:

button.userInteractionEnabled = NO;
jamapag
  • 9,290
  • 4
  • 34
  • 28
  • Perfect! Thanks - I should really have thought of that :D – Smikey Aug 12 '10 at 14:52
  • 1
    Well this one works but could have other issues since the touches on the button will not be forwarded to parent view. Button.adjustsImageWhenDisabled = NO; is better as @Snowman proposed – Ilker Baltaci Dec 07 '17 at 09:54
1

In interface builder select your button and then take a look at the button attributes. There is a drop down menu for default, highlighted, selected or disabled state configurations - select disabled and then adjust the alpha property.

Michael Behan
  • 3,433
  • 2
  • 28
  • 38
  • Aaaah, thanks, I've somehow never noticed that! However, when I select the disabled state configuration, nothing in the 'View' panel changes, the alpha is still at 1, and the checkboxes for 'opaque' etc remain the same regardless of which state configuration is selected. The only thing that changes is the Text Color and Shadow Color. So the problem remains... – Smikey Aug 12 '10 at 13:09
  • I couldn’t find a drop down menu like described in the current Xcode version. – ixany Feb 03 '17 at 20:34
  • With the button selected open the attributes inspector and you should see the drop down labeled "state config". (correct for Xcode 8.2.1 at least) – Michael Behan Feb 09 '17 at 11:24