gpt4 book ai didi

ios - 为 UITableCellView 内的 View 添加 NSLayoutConstraint

转载 作者:行者123 更新时间:2023-11-30 11:58:28 26 4
gpt4 key购买 nike

我想要一个 UIButton 位于 UITableCellView 的中心。我已在 tableView cellForRowAt 函数中添加了必要的代码,但出现错误:

View 层次结构没有为约束做好准备:当添加到 View 时,约束的项目必须是该 View (或 View 本身)的后代。如果在组装 View 层次结构之前需要解决约束,这将会崩溃。打断-[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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com