Avoiding making a UIButton transparent/see-through

2019-02-08 21:48发布

问题:

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.

Thanks all!!

Michael

回答1:

Try userInteractionEnabled property:

button.userInteractionEnabled = NO;


回答2:

You can also do:

button.adjustsImageWhenDisabled = NO;


回答3:

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.