ios - 在第二个动画之前查看跳跃

标签 ios swift nslayoutconstraint

我正在尝试为表格 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)>

enter image description here

最佳答案

问题很明显,您在点击此处的每个按钮时都设置了高度限制

 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/

相关文章:

ios - 停止 UITableView 部分标题在滚动时移动

ios - 添加文本时 UILabel 弹起

ios - 删除图表上线上的折线图值

ios - AdMob 广告测试横幅永远不会在中介中加载

swift - 使用 Alamofire 的 Swift 版本问题

ios - 动画约束变化,位置动画但高度跳跃没有动画

ios - 以编程方式获取 uibutton 的约束

ios - 如何在 .xib 文件中使用自定义 UITableView 和 UITableViewCell?

ios - 如何在 iOS 8 上 Touch ID 失败时回退到密码验证?

swift - IQKeyboardManager 不会用每个新文本行来提升 UITextView