2
votes

I have an UIScrollView in which I display tweets. The scrollView is contained in a stack of views, the last of which has 4 gesture recognizers.

The scrollView is populated as follows:

- (void) createTimeLine:(NSArray*)timeline
{
    CGRect rect = CGRectMake(0, 0, 463, 150);
    NSInteger i = 0;
    NSLog(@"timeline objects: %d", timeline.count);
    self.scrollView.contentSize = CGSizeMake(463, timeline.count * 150);
    for (NSDictionary *d in timeline) {
        SingleTwitViewController *c = [[SingleTwitViewController alloc] initWithAsingleTwit:d];
        rect.origin.y = i * 150;
        c.view.frame = rect;
        [scrollView addSubview:c.view];
        [scrollView bringSubviewToFront:c.view];
        i += 1;
    }
}

There are 20 objects in the timeline, and the first 4 are visible. However, the scrollView does not scroll and the touches are handeled by the gesture recognisers.

The scrollEnabled property is set to YES, but for some reason this does not seem to work.

The contentSize is set to 463 * 3000 which is much less than the scrollView it self, but still it won't scroll.

Any ideas?

2
Do you set the userInteractionEnable to YES for your scrollView and for your subView?MaTTP

2 Answers

0
votes

try these ,

scrollView.userInteractionEnabled=YES;

the content size should be greater than that of scrollView frame size... so set the content size greater...

0
votes

I also found a UIScrollView inside a simple view (part of the UIViewController) not responding to the touch events, even with the scrollView.userInteractionEnabled = true (regardless of the scrollView.contentSize). In a clean single-view project

This problem is not exactly as in the question above, but I found it interesting to see that my scrollView responds to scroll touches, but not to simple touches.

I decided to subclass the scroll view and add a protocol as follows:

protocol XXScrollViewDelegate {
    func scrollViewDidReportTouchesBegan()
}
class XXScrollView: UIScrollView {  
    var touchDelegate: XXScrollViewDelegate?    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.touchDelegate?.scrollViewDidReportTouchesBegan()
    }
}

Do not forget to declare the protocol in the main view, and set the delegate (note that I used a different name to avoid overriding the delegate property of the UIScrollView.

class ViewController: UIViewController, XXScrollViewDelegate {

scrollView.isUserInteractionEnabled = true
scrollView.touchDelegate = self

This solves the issue for me, but I am still curious on the reason why it does not respond as expected.