我已将 UIPickerView 添加到 UIScrollView 但现在 UPickerView 不滚动。当我将它添加到 self.view 时,它滚动顺畅。 这是我的代码
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];
我已经包含了委托(delegate)及其方法。看看这个问题。提前致谢。
如果我不清楚,请告诉我。
最佳答案
我使用 UIPickerView 的子类来达到相同的目的,但我的要简单得多:
@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
就像一个魅力 - 至少对我来说。
关于iphone - UIPickerView 添加到 UIScrollView 时不滚动!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793535/