3
votes

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)
    }
}
2
Try adding preSeasonView.userInteractionEnabled = trueSylvan D Ash
Have tried that with no effect, and it's working for the first few views.bobtune
@Snapper Did you find a solution for this problem?Omar HossamEldin

2 Answers

1
votes

I had the same problem, and the I had ContentView inside the Scroll View

The problem I discovered that the ContentView I set its height equal to the ScrollView parent view. And I was calculating the ContentSize of ScrollView my self.

So the behavior was that the scrollview is scrolling correctly but any view is OFF screen in first show of ViewController cannot detect touch.

After some debugging I tried to make ClipToBounds to be true for the ContentView. and I saw what I was waiting for, the content view is just having the height of the screen (ScrollView parent)

I removed the constraint which made the content view equal height of the scrollview parent. than added new constraint to align the bottom of the container view to the bottom of the most view at the bottom, and didn't calculate the content size any more.

Currently the scrolling is working correctly and the touch is working for all views.

0
votes

I had the same problem with the content view and I didn't have a constraint on the height.

What I ended up doing was removing the content view altogether and that fixed the problem.

Hope that helps!