如何设置UIDatePicker View的高度?

时间:2015-04-17 06:35:19

标签: ios objective-c uitableview animation hidden

我有静态UITableViewCell,其中包含隐藏的datePickerView。选择button后,datePickerView变为可见,cells高度变大。这是代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        if (self.datePickerView.hidden == YES) {
            return 252 - self.datePickerView.bounds.size.height;
        }
        else return 252;
    }
    tableView.estimatedRowHeight = 150;
    return UITableViewAutomaticDimension;
}

- (IBAction)datePicker:(id)sender
{
    if (self.datePickerView.hidden == YES)
    {
        [self.datePickerView setHidden:NO];

        [self.datePickerView setFrame:CGRectMake(self.datePickerView.frame.origin.x, self.datePickerView.frame.origin.y, self.datePickerView.frame.size.width, 0)];
        [UIDatePicker animateWithDuration:1.0 animations:^(){
            [self.datePickerView setFrame:CGRectMake(self.datePickerView.frame.origin.x, self.datePickerView.frame.origin.y, self.datePickerView.frame.size.width, self.datePickerView.frame.size.height)];
        }];
    } else {
        [self.datePickerView setHidden:YES];
    }
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

datePickerView变得可见,并且cells高度变大时,cells高度会动画,但datePickerView则不会。我试图设置其高度的动画,以便它应与cell同步,但它不是?

1 个答案:

答案 0 :(得分:0)

在动画中设置datePickerView的帧时,根本不会更改它。试试这个:

CGFloat originalHeight = self.datePickerView.frame.size.height;
[self.datePickerView setFrame:CGRectMake(self.datePickerView.frame.origin.x, self.datePickerView.frame.origin.y, self.datePickerView.frame.size.width, 0)];
[UIDatePicker animateWithDuration:1.0 animations:^(){
    [self.datePickerView setFrame:CGRectMake(self.datePickerView.frame.origin.x, self.datePickerView.frame.origin.y, self.datePickerView.frame.size.width, originalHeight)];
}];