开发者

UIDatePicker inside of a UIScrollView - iPhone application

How can I use a UIDatePicker inside of a UIScrollView in my iPhone app. I can't scroll through the dates o开发者_如何学Gon the picker, because it thinks my finger is controlling the UIScrollView instead. I'm trying to add a UIDatePicker to my UIScrollView, which is inside of my UIViewController.


You might try setting the scroll view's delaysContentTouches property to NO.

Better, though, would be to rethink your UI. A date picker takes up about half the screen on a small device. Even if you can get the date picker to scroll instead of the scroll view, you'll put the user in a position where it's going to be hard to scroll the scroll view. They'll have to figure out that they'll need to touch above or below the picker if they want to scroll the scroll view... it's just not going to be nice to use.

One solution might be to let the user tap a date, and then to present a modal view controller with a date picker. That'd let the user pick a new date without any ambiguity about scrolling.


After a few trials, I found the best solution to this problem was to subclass the scroll view that contains your date picker and do a hit test to see whether the user is trying to touch the picker. I found and adapted the code below from this question UIDatePicker inside UIScrollView with pages

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if ([result.superview isKindOfClass:[UIPickerView class]] || [result.superview isKindOfClass:[UIDatePicker class]])
    {
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = NO;
    }
    else 
    {
        self.canCancelContentTouches = YES; // (or restore bool from prev value if needed)
        self.delaysContentTouches = YES;    // (same as above)
    }
    return result;
}


another solution is pop it upward from bottom when you want to select data from picker. add to ur view (self.view).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜