【问题标题】:Add NSLayoutConstraint for a view inside a UITableCellView为 UITableCellView 中的视图添加 NSLayoutConstraint
【发布时间】:2017-11-28 08:45:25
【问题描述】:

我希望在我的 UITableCellView 中居中放置一个 UIButton。我在 tableView cellForRowAt 函数中添加了必要的代码,但出现错误:

视图层次结构没有为约束做好准备:当添加到视图时,约束的项必须是该视图(或视图本身)的后代。如果在组装视图层次结构之前需要解决约束,这将崩溃。打断 -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] 进行调试。

我的代码如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            cell = UITableViewCell(
                style: .default, reuseIdentifier: Id.settingButtonCellIdentifier)
            cell!.backgroundColor = UIColor.clear
            cell!.preservesSuperviewLayoutMargins = false
            cell!.separatorInset = UIEdgeInsets.zero

            let button : UIButton = UIButton(type: UIButtonType.custom) as UIButton
            button.backgroundColor = UIColor.red
            button.setTitle("Click Me !", for: UIControlState.normal)

            cell!.addSubview(button)

            button.translatesAutoresizingMaskIntoConstraints = false
            button.addConstraint(NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: cell!, attribute: .leading, multiplier: 1, constant: 10))
            button.addConstraint(NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: cell!, attribute: .trailing, multiplier: 1, constant: 10))
            button.addConstraint(NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: cell!, attribute: .top, multiplier: 1, constant: 10))
            button.addConstraint(NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: cell!, attribute: .bottom, multiplier: 1, constant: 10))
        }

        return cell!
    }

任何帮助将不胜感激!

【问题讨论】:

  • cell.contentView 中添加按钮以及将toItem 中的cell 替换为 cell.contentView 为。 @SeanLintern88 为什么要创建新单元格?你应该把它出队
  • 嗯,您当前没有从 tableview 中出列单元格,您只是初始化它们?我会更改为 tableView.dequeueCell。但在主题上,您需要设置按钮 autoTranslate off button.translatesAutoresizingMaskIntoConstraints = false
  • 请在 cell!.addSubview(button) 之前尝试这一行 button.translatesAutoresizingMaskIntoConstraints = false 并告诉我
  • 感谢您的快速回复!即使在 cell!.addSubview(button) 之前移动 button.translatesAutoresizingMaskIntoConstraints = false 也不能解决问题。在另一个主题上,我使用 dequeue 只是没有将其粘贴到此处以使代码专注于该问题
  • 我建议通过xib使用自定义单元格加载,然后您可以直接在storyboard中设置约束medium.com/@brianclouser/…

标签: ios swift uitableview uibutton nslayoutconstraint


【解决方案1】:

试试这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell = UITableViewCell(
            style: .default, reuseIdentifier: Id.settingButtonCellIdentifier)
        cell!.backgroundColor = UIColor.clear
        cell!.preservesSuperviewLayoutMargins = false
        cell!.separatorInset = UIEdgeInsets.zero

        let button : UIButton = UIButton(type: UIButtonType.custom) as UIButton
        button.backgroundColor = UIColor.red
        button.setTitle("Click Me !", for: UIControlState.normal)

        button.translatesAutoresizingMaskIntoConstraints = false


        cell!.contentView.addSubview(button)

        cell.contentView.addConstraint(NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1, constant: 10))
        cell.contentView.addConstraint(NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: cell.contentView, attribute: .trailing, multiplier: 1, constant: 10))
        cell.contentView.addConstraint(NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: cell.contentView, attribute: .top, multiplier: 1, constant: 10))
        cell.contentView.addConstraint(NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottom, multiplier: 1, constant: 10))
    }

    return cell!
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多