【问题标题】:Resize UITableView when I slide up a UIView, just like how it happens when the keyboard is shown当我向上滑动 UIView 时调整 UITableView 的大小,就像显示键盘时发生的情况一样
【发布时间】:2012-12-21 00:36:11
【问题描述】:

我正在向上滑动 UIView,它有一个 UIDatePicker 作为子视图。这是在我的UITableView 上方添加的,但不幸的是一些tableView 行仍在我的UIView 下方。

UITableView 用键盘向上推:

UITableView 没有被我的视图推高,覆盖了最后几个字段:

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

编辑:

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

【问题讨论】:

  • 您必须在添加日期选择器时为表格视图设置 frame/contentOffset。它不会自动为选择器执行此操作。

标签: objective-c ios cocoa-touch uitableview


【解决方案1】:

是的,这是可能的。作为向上滑动视图的动画的一部分,您可以继续动态更改 UITableView 的大小。如果你想做 UITableView 响应键盘的实际操作,不要管它的大小,而是改变它的内容插入和滚动指示器插入。在此处查看我的答案以及链接到的示例:

uitableview not resizing with keyboard

【讨论】:

    【解决方案2】:

    好吧,这比我想象的要容易。就我而言,当我触摸一行时,我希望选择器显示并且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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2011-07-10
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多