UIPickerView does not scroll when added to UIScrollView!

I added a UIPickerView to the UIScrollView, but now the UPickerView does not scroll. When I add it to self.view, it scrolls smoothly. Here is my code

monthsArray = [[NSArray alloc] initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];

UIPickerView *objPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(185,350,100,100)];
    objPickerView.userInteractionEnabled=YES;
    objPickerView.delegate = self;
    objPickerView.showsSelectionIndicator = YES; 
    [objScrollView addSubView:objPickerView];

I have included the delegate and its methods. take a look at this problem. Thanks in advance. If I do not understand, tell me.

+3
source share
6 answers

I use a subclass of UIPickerView for the same purpose, but mine is much simpler:

@implementation ScrollablePickerView

- (UIScrollView *)findScrollableSuperview {

    UIView *parent = self.superview;
    while ((nil != parent) && (![parent isKindOfClass:[UIScrollView class]])) {
        parent = parent.superview;
    }
    UIScrollView* scrollView = (UIScrollView *)parent;

    return scrollView;
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
    UIScrollView* scrollView = [self findScrollableSuperview];

    if (CGRectContainsPoint(self.bounds, point)) {
        scrollView.canCancelContentTouches = NO;
        scrollView.delaysContentTouches = NO;
    } else {
        scrollView.canCancelContentTouches = YES;
        scrollView.delaysContentTouches = YES;
    }

    return [super hitTest:point withEvent:event];
}

@end

It works like a charm - at least for me.

+3
source

From the UIScrollView class documentation:

: UIWebView UITableView UIScrollView. , , .

UIPickerView , , . , .

+1

, ( ):

http://www.alexc.me/uiscrollview-and-uidatepicker/153/

DelaysContentTouches CanCancelContentTouches , .

+1

UIPickerView . UIPickerView, .

0

UIPickerView, UIScrollView, UIPickerView. ContentTouches/CanCancelContentTouches , "" , " " - , ! , scrollView.

UIPickerView, , :

touchBegan UIPickerView UIView

0

. UIPickerView, , ( ), , .

I think UIPicker is looking for its parent chain to find out if there are any gesture recognizers, and sets itself as a delegate, so it can turn off gesture recognition further by the touch chain inside its borders and while the transformation changes the visual borders, it does not change the frame, does it not look for the original boundaries in it.

If this is not what is happening, you can use this to prevent the scroll view from being stolen by your touch.

I think I will switch to a custom UISCrollView instead of my converted UIPickerView.

0
source

Source: https://habr.com/ru/post/1787675/


All Articles