我想在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!
    }


任何帮助,将不胜感激 !

最佳答案

尝试这个

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!
}

关于ios - 在UITableCellView内部为 View 添加NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47527303/

10-14 20:07
查看更多