简体   繁体   English

如何在UITableView顶部显示UIView?

[英]How to show an UIView on top of an UITableView?

I have an UIViewController which contains a table view and a simple view. 我有一个UIViewController ,其中包含一个表视图和一个简单的视图。 Both of them are at the same level. 两者处于同一级别。

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. 当我这样做时,仅显示视图的1/4,而不显示完整的视图。

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

    })
}

And here is a screenshot with the view (below the buttons there should be an UIDatePicker which is not shown): 这是带有视图的屏幕截图(按钮下方应有一个未显示的UIDatePicker ):

在此处输入图片说明

Someone know what the issue is ? 有人知道这是什么问题吗? I am trying to do this from the storyboards. 我正在尝试从情节提要中执行此操作。

edit: 编辑:

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 :/ 显然,如果我增加高度,则显示的视图会更多,因此以某种方式表明显示的部分正好是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

    })
}

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. 如果您正在使用自动布局,我敢打赌,您应该这样做,那么,要做的最简单的方法就是切换视图的约束,请参见下面添加的gif。

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. 下面的演示也使用tableView。

Hope this helps. 希望这可以帮助。

在此处输入图片说明

See a demo here showHide that accomplish what you want 在此处查看演示showHide的演示,该演示可实现您想要的功能

在此处输入图片说明

Rather then transform, change your views center y position. 而是进行变换,更改视图中心y的位置。
ex: 例如:

@IBOutlet weak var viewToAnimateOutlet: UIView!
@IBAction func showViewButtonAction(_ sender: Any) {
    UIView.animate(withDuration: 1.5) {
        self.viewToAnimateOutlet.center.y -= self.viewToAnimateOutlet.frame.height
    }
        }
@IBAction func hideViewButtonAction(_ sender: Any) {
    UIView.animate(withDuration: 1.5) {
        self.viewToAnimateOutlet.center.y += self.viewToAnimateOutlet.frame.height
    }
}

What i did: 我做了什么:
I used autolayout and provided constraint for ViewToAnimate View is 我使用了自动布局并为ViewToAnimate提供的约束是

ViewToAnimates.leading = safeArea.leading "constant = 8" ViewToAnimates。Lead = safeArea。Lead“常数= 8”

ViewToAnimates.trailing = safeArea.trailing "constant = 8" ViewToAnimates.trailing = safeArea.trailing“常数= 8”

This constraint will place ViewToAnimate view outside of the main views bottom. 此约束会将ViewToAnimate视图放置在主视图底部之外。 so view will not visible until showViewButtonAction method called. 因此,在调用showViewButtonAction方法之前,视图将不可见。

ViewToAnimates.top = safeArea.bottom "constant = 0" ViewToAnimates.top = safeArea.bottom“常数= 0”

ViewToAnimates.height = 130 ViewToAnimates.height = 130

视图现在不在屏幕上

视图现在可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM