ios - 如何在 UITableView 之上显示 UIView?

标签 ios swift uitableview

我有一个 UIViewController,它包含一个 TableView 和一个简单 View 。两者处于同一水平。

在启动时,我的 View 开始隐藏在底部,当我按下按钮时,我希望我的 View 向上滑动。当我这样做时,只显示 1/4 的 View 而不是完整的 View 。

这在添加表格 View 之前工作正常,但现在我不明白为什么它没有完全显示。

这是显示和隐藏我的 View 的代码:

func showPicker(date: Date?) {
    UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
        self.timePickerView.transform = CGAffineTransform(translationX: 0, y: 0)
    }, completion: { _ in

    })
}

func hidePicker() {
    UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
        self.timePickerView.transform = CGAffineTransform(translationX: 0, y: self.timePickerView.frame.size.height)
    }, completion: { _ in

    })
}

下面是 View 的屏幕截图(按钮下方应该有一个未显示的 UIDatePicker):

enter image description here

有人知道是什么问题吗?我正在尝试从 Storyboard 中做到这一点。

编辑:

这就是我现在拥有的,但它仍然不起作用。它没有动画,它也只显示 View 的一部分。显然,如果我增加高度, View 会显示得更多,所以不知何故它说显示的部分恰好是 220 高度,这很奇怪:/

func hidePicker() {
   self.pickerBottomConstraint.constant = -220
    UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
        self.timePickerView.layoutIfNeeded()
    }, completion: { _ in

    })
}

func showPicker(date: Date?) {
    self.pickerBottomConstraint.constant = 0
    UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
        self.timePickerView.layoutIfNeeded()
    }, completion: { _ in

    })
}

最佳答案

如果您正在使用自动布局,我敢打赌您会这样做,并且您应该这样做,那么做您想做的事情的最简单方法就是切换 View 的约束,请参见我在下面添加的 gif。

首先是引用您要显示和隐藏的 View 的顶部或底部约束。然后修改约束的常量来调整它的位置,这样你就会得到 View 隐藏和显示的错觉。下面的演示也使用了 tableView。

希望这对您有所帮助。

enter image description here

关于ios - 如何在 UITableView 之上显示 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49597822/

相关文章:

ios - 如何让用户在保存时自定义文件名?

ios - 当前选择的标签栏为标签时,如何滚动到 UITableViewController 的顶部? ( swift )

android - Expo:React Native 中 VideoPlayer 上的自定义按钮

Swift 等同于 __attribute((objc_requires_super))?

iOS UIView 阴影在模拟器中显示不同

iphone - 未找到存档文件

ios - 如何在新版本上转换 NSKeyedArchiver 对象

ios - iPhone 应用程序启动时无法从 Watch 读取 NSUserDefaults

ios - 在回调中使用函数/在单元格内使用 SegmentedControl

swift - 从非 tableview 类更改自定义 UITableViewCell 标签会导致错误