5

I have a UIButton that has four child UILabels that contain information about the button's functionality. When I click the center of the button the action does not fire because it is being blocked by the UILabels but when I click the outside of the button the action does fire. Is there a way to prevent the UILabels from blocking the action firing?

KevinZ
  • 756
  • 1
  • 12
  • 26
  • 1
    have you tried disable user interaction for that label by setting `.isUserInteractionEnabled = false`? – Tj3n Feb 27 '17 at 02:58
  • How could you fire the action when click OUTSIDE of the button? You sure about this? – Yun CHEN Feb 27 '17 at 03:01
  • @YunCHEN There is a margin of 5 between the UILabels and the sides of the UIButton so when I click in the margin the action fires – KevinZ Feb 27 '17 at 03:03
  • @SmedleyDSlap, for me, the label does not influence the button, could you show us you code that adds label into the button? – Yun CHEN Feb 27 '17 at 03:10
  • 1
    @Tj3n You were correct I just forgot to set .isUserInteractionEnabled = false to the UIStackView that organized the UILabels – KevinZ Feb 27 '17 at 03:10

1 Answers1

19

You'll need to set isUserInteractionEnabled = false on any view that is above the button, or a subview of it.

By default UILabel have it set to false, but as you mentioned in the comments, UIStackView does not. So calling isUserInteractionEnabled = false on it will do the trick ┌( ಠ‿ಠ)┘

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • 1
    Ahh, for so long time I never tested touch handles above another I never tought that the touch would "go through" to the view below it. :) Upvoted , deleted my answer. –  Feb 27 '17 at 03:18