I have a UIScrollView
in my view controller with a UIView
(called viewPreSeasonCard
) in it acting as the content view, all done in Interface Builder. I am then programmatically adding sub views to the container like this:
func displayPreSeason(preSeasons: [PreSeason]) {
var yPos = 0
let viewWidth = Int(viewPreSeasonCard.frame.width)
for (index, preSeason) in preSeasons.enumerated() {
yPos = 40 + index * 80
let frame = CGRect(x: 0, y: yPos, width: viewWidth, height: 78)
let preSeasonView = PreSeasonLineupView(frame: frame)
preSeasonView.setPreSeason(preSeason: preSeason)
preSeasonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.preSeasonClicked)))
viewPreSeasonCard.frame.size.height += frame.height
viewPreSeasonCard.addSubview(preSeasonView)
}
let curSize = self.view.frame
scrollView.contentSize = CGSize(width: curSize.width, height: curSize.height + CGFloat(yPos))
}
As you can see I am then adjusting scrollView.contentSize
after adding the subviews. This all works properly, I can scroll the scrollview all the way down and see all the subviews.
The problem is with the UITapGestureRecognizer
I am adding to the subviews. When the subview is initially visible on the device screen (i.e. the first 3 or 4 subviews) the gesture recognizer is working. But when I have to scroll to see subviews, the gesture recognizers on these subviews is not firing at all when I tap on them. It's as if because the lower subviews are not visible initially, the gesture recognizer is ignored.
Here's the method for the gesture recognizer:
func preSeasonClicked(_ sender: UITapGestureRecognizer) {
if let preSeasonView = gestureRecognizer.view as? PreSeasonLineupView, let constructorId = preSeasonView.constructorId {
presenter.preSeasonClicked(constructorId: constructorId)
}
}
preSeasonView.userInteractionEnabled = true
– Sylvan D Ash