我正在尝试为表格 View 设置动画,使其看起来像下拉菜单, View 显示正常(尽管我更喜欢从上到下显示,目前是从中间向外显示)问题是当我点击按钮关闭它时表格 View 再次跳跃然后消失然后按下按钮之后什么都不做。第二次点击按钮后出现约束错误。
这就是所谓的点击按钮:
let height: CGFloat = tableViewOpen ? 0 : 300
UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: {
self.tableView.set(height: height)
self.tableView.layoutIfNeeded()
}).startAnimation()
tableViewOpen = !tableViewOpen
我对 tableView 的约束是使用我工作的公司的一些扩展设置的,但看起来像这样:
tableView.pin(.top, to: .bottom, of: breedButton)
tableView.pin(toSuperview: [.leading, .trailing])
tableView.set(width: 250)
更多详细信息 - 表格 View 位于 subview Controller 中,该 Controller 只是品种按钮和表格 View 。 subview Controller 约束设置如下:
filterViewController.view.pin(.top, to: .top, of: titleLabel)
filterViewController.view.pin(toSuperview: .leading)
filterViewController.view.set(width: 250)
没有为 tableview 或 childVC 设置高度,不确定是否是这个问题。
第二次点击按钮后的约束错误:
(
"<NSLayoutConstraint:0x600003e53a20 UITableView:0x7fe090829000.height == 300 (active)>",
"<NSLayoutConstraint:0x600003e2c050 UITableView:0x7fe090829000.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003e53a20 UITableView:0x7fe090829000.height == 300 (active)>
最佳答案
问题很明显,您在点击此处的每个按钮时都设置了高度限制
self.tableView.set(height: height)
但是你应该创建一次高度限制
var tableHeight:NSLayoutConstraint!
并将其设置在 viewDidLoad
中,然后将其更改为常量
tableHeight.constant = show ? 300 : 0
UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: {
self.view.layoutIfNeeded()
}).startAnimation()
关于ios - 在第二个动画之前查看跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55544335/