UITextView in UIScrollView is not scrollable

2019-07-28 04:18发布

问题:

I have a UITextView in my UIScrollView. The Scrollview contains images and scrolls horizontally. The Textview is added as a subview and contains some text. Sometimes the text is quite long, so I want the textview to be scrollable. Here is what I do:

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(260, 490, 280, 86)];
    [textView setBackgroundColor:[UIColor colorWithRed:63.0f/255.0f  green:154/255.0f blue:201/255.0f alpha:1.0f]];
    [textView setTextColor:[UIColor whiteColor]];
    [textView setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
    [textView setScrollEnabled:YES];
    [textView setUserInteractionEnabled:YES];
    [textView setContentSize:CGSizeMake(280, 400)];

Problem is, I cannot scroll.

Is it possible, that the scrollview eats my touches? What would be a clean solution for this?

回答1:

UIScrollViews have a bolean property called delaysContentTouches. By default, this is set to YES - it tells the scroll view to delay passing on touch events to its subviews.

This is why currently your text view doesn't scroll. If you set delaysContentTouches to NO you will hopefully find your text view now working.



回答2:

I've put horizontal-only UISCrollViews in a vertical-only UIScrollView. That works fine. But if the parent and child UISCrollView both support scrolling in the same direction, I could see that being a problem. You may need to sub-class UIScrollView and override touch events and dispatch them appropriately (to the parent or child, depending on what you want to happen, where the touch is, etc)