4

I had a custom view with subclassed touch responses that was working in iOS 4. On iOS 5, these touches would not respond at all when touched along the bottom edge of the view, if the view's background color was set to clearColor.

I have not been able to track this down, but does anyone know if iOS 5 changed the way views respond to touches depending on a transparent background?

I can make no changes to the code other than set the background color to any opaque color like orangeColor and the view fully responds.

Note the issue does not affect touches elsewhere in the view; only along the bottom edge, anywhere below the last subview added to the view; presumably a clear background is treated as if the view does not exist for the sake of touches when looking at an area of the view that has no content. Change the color, the view has "content" and the touches work!

johnbakers
  • 24,158
  • 24
  • 130
  • 258

2 Answers2

14

Instead of using [UIColor clearColor], try using this:

[view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]];

NOTE: A UIView does not respond to touch events when the alpha is anything below 0.1. [UIColor clearColor] sets an alpha to 0.0, so you won't get the touch events. Following the above method, you can receive the touch events on a transparent view.

skram
  • 5,314
  • 1
  • 22
  • 26
  • nice suggestion but makes no difference – johnbakers Jun 13 '12 at 09:46
  • Touches are ignored when the alpha value is less than `0.1`. Try setting it to `0.1`. If this works I will edit my answer to reflect that – skram Jun 13 '12 at 09:54
  • my view has an alpha of 1.0; it is visible and its subviews are visible, but the background color is `clearColor` – johnbakers Jun 13 '12 at 10:18
  • I know, set the alpha on the UIColor, from my posted answer to 0.1. I don't mean the alpha on the view itself.. – skram Jun 13 '12 at 10:23
  • wow! that did the trick. please update your answer and I will accept it. thanks! – johnbakers Jun 13 '12 at 12:59
  • 2
    It seems at least with iOS 8 that touch events are triggered with alpha set to 0.01, no longer does it need to be 0.1. – Jordan H Jul 14 '14 at 04:47
  • 1
    Problem is, even alpha at 0.01 affects how the view **looks**, so it's not a good solution. What kind of weird optimization is this, anyway? It seems to bypass hitTest entirely. I even tried adding an extra subview to the transparent subview — itself also transparent — and hitTest wasn't triggered even then. I guess it's reading the actual pixel values of the rendered view before performing any hit detection? Why isn't this behavior described in the documentation?! – Archagon Jul 24 '14 at 21:20
5

In case anyone else runs into this problem and wants a better solution than setting a partial opacity for the background, you can set the view's opaque property to NO and then add an empty drawRect: method. (Tested and working on iOS8, beta 4.)

nrj
  • 1,701
  • 2
  • 22
  • 37
Archagon
  • 2,470
  • 2
  • 25
  • 38