0

I have a view in my storyboard that by default the alpha is set to 0. In certain cases the Swift file sets the alpha to 1. So either hidden or not. Before this view just contained 2 labels. I'm trying to add 2 buttons to the view.

For some reason the buttons aren't clickable at all. So when you tap it normally buttons change color slightly before you releasing, or while holding down on the button. But that behavior doesn't happen for some reason and the function connected to the button isn't being called at all.

It seems like an issue where something is overlapping or on top of the button. The button is totally visible and enabled and everything but not clickable. I tried Debug View Hierarchy but everything looks correct in that view.

Any ideas why this might be happening?

EDIT I tried making a class with the following code and in interface builder setting the container view to be that class.

class AnotherView: UIView {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        for view in self.subviews {
            if view.isUserInteractionEnabled, view.point(inside: self.convert(point, to: view), with: event) {
                return true
            }
        }

        return false
    }
}
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • Have you got `enabled` and `User Interaction Enabled` checked on the Storyboard? – Nicholas Smith Jul 29 '17 at 17:18
  • @NicholasSmith Yes, both are checked – Charlie Fish Jul 29 '17 at 17:19
  • @NicholasSmith Oh I realized the view has `User Interaction Enabled` disabled. I didn't realize that would be inherent for subviews. Is there a way to only enable that for the buttons and not the entire view? It's on top of a UITableView so I want users to be able to scroll on the table view if they drag on the labels. If that makes sense. – Charlie Fish Jul 29 '17 at 17:48
  • @CharlieFish: For your issue, refer:https://stackoverflow.com/questions/5887305/uiview-user-interaction-enabled-false-on-parent-but-true-on-child – Puneet Sharma Jul 29 '17 at 17:54
  • @CharlieFish: don't quote me on it but I think you might have to use `UIGestureRecognisers` if the solution from @PuneetSharma doesn't work. – Nicholas Smith Jul 29 '17 at 17:58
  • @PuneetSharma Seems like [this](https://stackoverflow.com/a/40805873/894067) should work. But when I try to write a UIView extension with that code in it, I get an error `Method does not override any method from its superclass`. I don't want to create multiple views where one has touch enabled and another has touch disabled. That will cause problems for my project and the behavior I'm trying to achieve. – Charlie Fish Jul 29 '17 at 18:14
  • @CharlieFish: You need to create a separate UIView class, and override the hitTest function. Show us the code. – Puneet Sharma Jul 29 '17 at 18:16
  • @PuneetSharma Just edited my question with the class code – Charlie Fish Jul 29 '17 at 18:18
  • @PuneetSharma Still not working tho – Charlie Fish Jul 29 '17 at 18:18
  • @PuneetSharma So I tried setting a breakpoint on the first line within that function. It doesn't get hit at all. But if I enable `User Interaction Enabled` on the view it gets hit. Which I really don't want. – Charlie Fish Jul 29 '17 at 18:23
  • @CharlieFish: added a code snippet. I have tested it ,its working. – Puneet Sharma Jul 29 '17 at 18:36

1 Answers1

2

Go with hitTest(_:with:) method. When we call super.hitTest(point, with: event), the super call returns nil, because user interaction is disabled. So, instead, we check if the touchPoint is on the UIButton, if it is, then we can return UIButton object. This sends message to the selector of the UIButton object.

class AnotherView: UIView {

    @IBOutlet weak var button:UIButton!

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if self.button.frame.contains(point) {
            return button
        }
        return view
    }

    @IBAction func buttnTapped(sender:UIButton) {

    }
}
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33