I have a few buttons in my app whose alpha is currently set to zero. These buttons are completely non-responsive, but as soon as I increase their alpha, they begin to respond. Is this expected behavior?
4 Answers
As per Apple's documentation for UIView
's hitTest:withEvent:
method:
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01.
So any UIView
that has alpha lower than 0.01 will be ignored by the touch events processing system, i.e. will not receive touch.
-
14I tried 0.01 exactly and it disabled the touch events. It has to be at least 0.02– CSawyAug 13, 2014 at 18:15
-
5@CSawy I tried 0.010001 and it enabled the touch events. so it has to be greater than 0.01 Oct 5, 2019 at 20:42
I'm a little late to the game but you could always set the UIButton background color to clearcolor. This would keep them active. In my case, I am pulsating a button to give it a glowing effect but to do this, I must set it's background to clear, then add a UIIMageView as a subview and add the effect to the image, NOT the button.
Hope this helps anyone else with this problem.
When the alpha is 0 they are unresponsive, as alpha = 0 is like hidden = YES and you can't click a hidden button.
-
5This is an old answer, but don't you mean to say "When the alpha is 0 they are unresponsive..."– Duncan CMay 15, 2017 at 14:07
-
I just tried to edit this answer replacing "responsive" with "unresponsive" but got "edits must be more than six characters". This Stack Overflow requirement is especially bad considering that this answer was the one chosen by google in their snippet up the top of their search results. And no-one can correct it.– tomblahFeb 4, 2020 at 5:29
Yes, the least alpha amount should be 3.0 / 255.0
for touch event to not be ignored.