当我向上滑动UIView时调整UITableView的大小,就像显示键盘时的情况一样

时间:2012-12-21 00:36:12

标签: objective-c ios cocoa-touch uitableview

我正在滑动UIView,其UIDatePicker为子视图。这已添加到UITableView之上,但不幸的是,某些tableView行仍在我的UIView下。

UITableView被键盘推高:

enter image description here

UITableView不会随我的观点推升,覆盖了最后几个字段:

enter image description here

当我向上滑动视图时,是否可以动态调整UITableView的大小,就像在tableView中显示键盘并且仍能看到所有行时一样?

修改

刚刚找到一个很棒的Apple示例:http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html

2 个答案:

答案 0 :(得分:0)

是的,有可能。您可以继续动态更改UITableView的大小,作为向上滑动视图的动画的一部分。如果您想要执行UITableView实际响应键盘的操作,请单独保留其大小,而是更改其内容插入和滚动指示符插入。请参阅我的答案和链接到的示例:

uitableview not resizing with keyboard

答案 1 :(得分:0)

好吧,这比我想象的容易。在我的情况下,当我触摸一行时,我希望选择器显示并tableView更改其高度,所以我使用:

[UIView animateWithDuration:0.4 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{

    self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height-200, self.view.bounds.size.width, self.pickerView.frame.size.height);

    // shrink the table vertical size to make room for the date picker
    CGRect newFrame = self.tableView.frame;
    newFrame.size.height -= self.pickerView.frame.size.height;
    self.tableView.frame = newFrame;

} completion:nil];

然后当我点击完成按钮,并希望将tableView返回到完整高度并隐藏我的datePicker时,我使用:

- (void)doneWithPicker:(BOOL)remove {

    CGRect newFrame = self.tableView.frame;
    newFrame.size.height += self.pickerView.frame.size.height;
    self.tableView.frame = newFrame;

    [UIView animateWithDuration:0.4 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{
        self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height+300, self.view.bounds.size.width, self.pickerView.frame.size.height);
    } completion:^(BOOL finished){
        if (remove) {
            [self.pickerView removeFromSuperview];
            self.pickerView = nil;
        }
    }];

}

此外,将pickerView添加为子视图时,请务必将其添加到window

[self.view.window addSubview:self.pickerView];