I moved all my UIElements in a scrollView to avoid a loaded UIViewcontroller. But now the GestureRecognizers on the view does not work anymore.
My View hierarchy looks like this:
- UIViewController (SingleEventController)
- UIScrollView (EventScrollView)
- UIView (contentView)
- UILabel, UIView, UITableView, etc.
This looks like a common problem because I found quite much on Stackoverflow like: Example 1 Example 2. Still, I was not able to solve my case..
Simplified code in my EventScrollView looks like this:
let locationLabel: UILabel = {
let label = UILabel()
label.text = "Standort"
label.isUserInteractionEnabled = true //Important!
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true //not needed!
contentView.isUserInteractionEnabled = true //Important!
}
func setupViews() {
guard let parent = parentVC else { return }
let locationTap = UITapGestureRecognizer(target: parent, action: #selector(parent.openInGoogleMaps))
locationTap.cancelsTouchesInView = false //Important!
addSubview(contentView)
contentView.addSubview(locationLabel)
locationLabel.addGestureRecognizer(locationTap)
}
What step am I missing?
Btw, clicks on a row of a UITableView
inside the contentView don't get registered either.