【问题标题】:How to present a UIPicker above a table view?如何在表格视图上方显示 UIPicker?
【发布时间】:2011-03-28 23:57:04
【问题描述】:

我有一个从 nib 文件实现表视图的视图控制器,它的视图出口直接连接到 Interface Builder 中的表视图。

在某些情况下,我尝试将 UIDatePicker 从屏幕底部滑动到视图中,允许用户进行选择,然后将其向下滑动。

我已经让选择器滑入屏幕,但是我的问题是,因为我需要将它添加为像这样的子视图,[self.view addSubview:pickerContainer]; 正在发生的事情是选择器被添加到表视图中,每当我尝试更改它的值,整个选择器会随着表格上下滑动。

我应该怎么做才能在表格视图上方显示 DatePicker?

【问题讨论】:

    标签: iphone ios4 iphone-sdk-3.0


    【解决方案1】:

    听起来你应该以模态的方式呈现它。

    [pickerContainer setModalPresentationStyle:UIModalPresentationCurrentContext];
    [self presentModalViewController:pickerContainer animated:YES];
    

    并放置一个完成/选择/等按钮,在调用时执行此操作:

    [self dismissModalViewControllerAnimated:YES];
    

    编辑: 另一种选择是让父 UIViewController 同时具有表视图和另一个隐藏视图。将隐藏视图放置在屏幕底部(位于表格视图的顶部),确保控件开始禁用,然后调用:

    [UIView animateWithDuration:1.0 /* set number of seconds it takes to slide here */
     animations:^
     {
         [pickerContainer setFrame:/*the position you want it to slide in to*/];
     }
     completion:^(BOOL finished)
     {
         [pickerContainer.picker setEnabled:YES]; // only enabled once the animation is done
         [pickerContainer.doneButton setEnabled:YES]; // same for the button to close the view
     }];
    

    当用户表示他们已完成选择日期时,再次使用 setFrame: 动画回到视图的原始位置以将其滑出,并禁用控件。

    【讨论】:

    • 这会覆盖整个屏幕吗?我宁愿只让它覆盖我的视图的底部......
    • 添加了一个可能对您有所帮助的替代解决方案。
    【解决方案2】:

    从 iOS 3.2 开始,您可以将 UITextFieldinputView 属性设置为 UIPickerView。这将用您应用于inputView 的任何视图替换键盘,并在该 textField 成为 firstResponder 时显示此视图。

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2015-11-30
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多