带有日期的UIPickerView仅在一个方向上旋转

时间:2010-10-14 12:58:19

标签: iphone uipickerview

我遇到了UIPickerView的奇怪行为:

我正在尝试在操作表中显示日期选择器。 一切都运作良好,除了车轮首先只向一个方向旋转!

如果你开始向上旋转并将方向改为向下 - 它可以工作。但是当你试图开始向下旋转时,拾取器将不会移动。

这是我的代码:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Abbrechen"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"Auswählen",nil];    
    // Add the picker
    UIDatePicker *tmpPickerView = [[UIDatePicker alloc] init];
    tmpPickerView.datePickerMode = UIDatePickerModeDate;

    [tmpPickerView addTarget:self
    action:@selector(pickerChanged:)
    forControlEvents:UIControlEventValueChanged]; 

    [menu addSubview:tmpPickerView];              
    [menu showInView:self];        

    CGRect menuRect = menu.frame;
    menuRect.origin.y -= 214;
    menuRect.size.height = 300;
    menu.frame = menuRect;

    CGRect pickerRect = pickerView.frame;
    pickerRect.origin.y = 174;
    pickerView.frame = pickerRect;

    [tmpPickerView release];    
    [menu release];

1 个答案:

答案 0 :(得分:0)

问题是UIDatePicker超出了UIActionSheet的范围,如果你设置了

,你可以看到

menu.clipsToBounds = YES;

为了适当调整大小,请将帧大小调整代码更改为

CGRect pickerRect = tmpPickerView.frame;
CGRect menuRect = menu.frame;

pickerRect.origin.y = menuRect.size.height;
menuRect.size.height = menuRect.size.height + pickerRect.size.height;
menuRect.origin.y = menuRect.origin.y - pickerRect.size.height;

menu.frame = menuRect;    
tmpPickerView.frame = pickerRect;