Here’s my view hierarchy: parentView (UIView) has a UIImageView as its subview which in turn has a UIButton as its subview.
left = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left.png"]];
left.userInteractionEnabled = YES;
[parentView addSubview:left];
back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setImage:[UIImage imageNamed:@"Arrow-left.png"] forState:UIControlStateNormal];
[back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
back.showsTouchWhenHighlighted = YES;
[left addSubview:back];
Everything shows up properly but the button does not respond to touches. It does respond if I move its frame out of the UIImageView’s frame to somewhere else and set it as a subView to the parentView (UIView). But here’s the thing.
Even if I set to parentView’s subView the button does not respond if it is within the UIImageView’s frame area. The userInteractionEnabled property has been already set to YES for the image view. Any idea what’s going on?
UIImageView turns off userInteraction – turn it on and the button will work.
EDIT:
So I used your code almost exactly as written – the one red herring is that you said it all appears fine. For me, the custom button had a frame of 0,0,0,0 so I saw nothing. When I set the frame it all worked perfectly:
So, if you need to probe the superviews during run time to figure out what is what, you can use this code below. [UIView dumpSuperviews:back msg:@”Darn Bark Button”];