首页 文章

更改故事板约束关系

提问于
浏览
1

我在故事板上更改约束的关系时遇到了麻烦 . 这就是表视图目前的样子 .
enter image description here

单击更多按钮时,我希望表视图看起来像这样 .
enter image description here

好的,我将底部约束从imageView拖到我的tableViewCell.swift文件中,并将其命名为 imageBottomConstraint . 我试过了 imageBottomConstraint.relation = .greaterThan ,但是给了我一个错误,说它试图将它改为更大的东西's only a get, so the relation is equalTo and I' .

这是我试图创建一个新的约束,但不是真的理解 NSLayoutConstraint(item:, attribute:, relatedBy:, toItem:, attribute:, multiplier:, constant:) . 不懂项目,属性,relatedBy,toItem&attribute,有人可以帮忙澄清一下 .

我认为我的灾难代码会起作用,但不会崩溃 .

@IBAction func showMoreBtn() {
    let item = imageBottomConstraint.firstItem
    let attribute1 = imageBottomConstraint.firstAttribute
    let relatedBy = NSLayoutRelation.greaterThanOrEqual
    let item2 = imageBottomConstraint.secondItem
    let attribute2 = imageBottomConstraint.secondAttribute
    let multiplier = imageBottomConstraint.multiplier
    let constant = 12 

    let newConstraint = NSLayoutConstraint(item: item, attribute: attribute1, relatedBy: relatedBy, toItem: toItem, attribute: attribute2, multiplier: multiplier, constant: 12)
}

因此,当您创建约束程序时,您可以看到我不知道我在做什么 . 很感激帮助 .

2 回答

  • 0

    您可以在TableView的heightForRowAt Cell Delegate中管理预期行为

    /// Use this function to Set Height of TableCells
            func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
            {
                /// Is index Path More Button is cliked ?
                if indexPath.row == selectedIndex
                {
                    /// If yes
                    return UITableViewAutomaticDimension
                }
                else
                {
                    /// if No
                    /// Provide a Static Height That will Set your requiremment
                    return 160
                }
            }
    

    selectedIndex 其中更多Button被点击的索引,我的要求是扩展一个单元格一次,你可以使用一个数组来管理单元格需要扩展

  • 1

    您可能无法使用正确的解决方案 . 我会通过使用 UITableViewAutomaticDimension 来使用单元格可以扩展并适应内容的事实 . 这是一个可以帮助您解决特定情况的示例:

    https://www.appcoda.com/self-sizing-cells/

    这个想法是:

    • 使用约束来修复标签的高度

    • 单击更多按钮时,使用 yourConstraint.active = false 将该约束设置为不活动,并通过执行类似 yourTableView.reloadRows 的操作重新加载单元格

相关问题