如何在 UITableView 之上显示 UIView? [英] How to show an UIView on top of an UITableView?

查看:20
本文介绍了如何在 UITableView 之上显示 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,它包含一个表格视图和一个简单的视图.两者处于同一水平.

I have an UIViewController which contains a table view and a simple view. Both of them are at the same level.

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

At startup my view starts hidden at the bottom and when I press a button I want my view to slide up. When I do this only 1/4 of the view is shown and not the complete view.

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

This worked okay before adding the table view, but now I don't understand why it doesn't fully show.

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

Here is the code to show and hide my 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

    })
}

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

And here is a screenshot with the view (below the buttons there should be an UIDatePicker which is not shown):

有人知道这是什么问题吗?我正在尝试从故事板中做到这一点.

Someone know what the issue is ? I am trying to do this from the storyboards.

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

This is what I have right now, but it still doesn't work. It doesn't animate and it also shows just a part of the view. Apparently if I increase the height the view is shown even more, so somehow it says that the shown part is exactly 220 height, which is strange :/

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

    })
}

推荐答案

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

If you're using autolayout, I bet you do and you should, then the easiest way to do what you wanna do is to toggle the constraint of your view, see the gif I added below.

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

First is to have a reference to your either top or bottom constraint of your view you wanna show and hide. Then modify the constant of the constraint to adjust its position, in that way, you get the illusion that the view is hidden and shown. The demo below uses tableView too.

希望这会有所帮助.

这篇关于如何在 UITableView 之上显示 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆