1
votes

I have a UIButton in a View and over that button I have a UILabel (TTTAttributedLabel). (https://github.com/mattt/TTTAttributedLabel) (Though, this is not a direct question related to TTTAttributedLabel custom class)

This UILabel performs some actions with touch and If doesn't like touch coordinates it forwards them to super with the followings in corresponding methods:

    [super touchesBegan:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
    [super touchesCancelled:touches withEvent:event];

I see that the button below is being pressed in that case. I also see that all of those methods in UIButton is getting called perfectly. I also log UITouch objects to see if they match.

The problem is, the action set to UIButton is not getting called (even for UIControlEventTouchDown). What does trigger those actions? I thought that a matching touchesBegan with touchesEnded should have called the selector added for target action to that button.

1

1 Answers

0
votes

Touches trigger the control events, and I would say your button does not receive touches simply because there is a label over a button.

There are two ways you could go around this. First, and probably the preferred way is to use the atributedTitle proprety of a UIButton instead of mashing an additional label where it's not necessary: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/attributedTitleForState:

The other way is to delve deeper into UIControl, possibly subclass it, and then handle the touch events on a bit of a finer scale, although this is far more complex than the solution above: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/reference/reference.html