1
votes

I have an UIButton located inside of an UIView subview. The view appears fine but the button is not responding to touch. The subviews are two separate ViewControllers embedded into the container views.

On the storyboard all views, controllers, and buttons have user interaction enabled. I have also enabled user interaction for the subviews programmatically.

Here is the code:

@IBOutlet var showtimeView: UIView!
@IBOutlet var reviewView: UIView!
var viewTwo: UIView!
var viewThree: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    viewTwo = ShowtimesView().view
    showtimeView.addSubview(viewTwo)
    viewThree = ReviewsView().view
    reviewView.addSubview(viewThree)
    showtimeView.isUserInteractionEnabled = true
    reviewView.isUserInteractionEnabled = true
    viewTwo.isUserInteractionEnabled = true
    viewThree.isUserInteractionEnabled = true


}

@IBAction func segmentChange(_ sender: Any) {

    switch(segment.selectedSegmentIndex) {

    case 0:

        detailsView.isHidden = false
        showtimeView.isHidden = true
        reviewView.isHidden = true

        break

    case 1:

        detailsView.isHidden = true
        showtimeView.isHidden = false
        reviewView.isHidden = true

        break

    case 2:

        detailsView.isHidden = true
        showtimeView.isHidden = true
        reviewView.isHidden = false

        break

    default:
        break

    }

}

What else could be preventing the UIButton located inside of the subview from responding to touch?

EDIT: I changed the color of the container to check if it was covering the viewcontroller. The container did not show up in front of the subview viewcontroller. The subviews still do not respond to touch.

2
As @smartcat mentioned, your buttons could be covered up by a clear view. Add a screenshot of your view hierarchy as shown in Interface Builder.toddg

2 Answers

0
votes

If there is another view on top of the button, such as a container view and the view within that container view (and any other view of that container view that covers the button), then touch events won't make it to that button.

To compensate, have the container view (that's covering your button) and the view within it both derive from the following view:

class PassthroughView: UIView
{
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
    {
        let view = super.hitTest(point, with: event)
        return view == self ? nil : view
    }
}

You can make the change from UIView to PassthroughView for those two views directly in the storyboard, and just toss the above class somewhere (I prefer it to be in its own file).

0
votes

The solution was to delete the code in viewDidLoad. Since the views are already embedded into the container views through storyboard segues they don't need to be created as subviews.