Ran*_*rns 4 constraints uitableview ios swift
这一切都发生在这个类中:
class CustomCell2: UITableViewCell {
Run Code Online (Sandbox Code Playgroud)
当我打印 Storyboard 中设置的现有宽度约束的值(标签 = 0.7 * 单元格宽度)时,我看到以下内容:
<NSLayoutConstraint:0x36b8750 UILabel:0x3d2aac0'Label'.width == 0.7*appName.CustomCell2:0x3e01880'CustomCell2'.width>
Run Code Online (Sandbox Code Playgroud)
当我尝试以编程方式创建相同的约束时,出现错误“视图层次结构未为约束做好准备:”
<NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width>
Run Code Online (Sandbox Code Playgroud)
看起来完全一样,那为什么不能添加这个约束呢?
CustomCell2的awakeFromNib()中的约束代码:
let widthConstraint = NSLayoutConstraint(item: labelName, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.9, constant: 0)
labelName.addConstraint(widthConstraint)
Run Code Online (Sandbox Code Playgroud)
错误消息的其余部分:
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2017-12-03 20:18:51.183 appName[899:684382] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width>
Container hierarchy:
<UILabel: 0x3d2aac0; frame = (281 43; 674 54); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x4233d30>>
View not found in container hierarchy: <appName.CustomCell2: 0x3e01880; baseClass = UITableViewCell; frame = (0 0; 963 160); layer = <CALayer: 0xee08250>>
That view's superview: NO SUPERVIEW
Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width> view:<UILabel: 0x3d2aac0; frame = (281 43; 674 54); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x4233d30>>
Run Code Online (Sandbox Code Playgroud)
谢谢!
您应该在单元格的 init 中添加约束,假设它将是一个出队的可重用单元格,请记住使用 contentView 而不是 View 作为单元格的边界:
\n\nclass CustomCell2: UITableViewCell {\n\n//MARK: Subviews\n//Add View Programmatically or in xib\nlet titleLabel: UILabel = {\n let label = UILabel()\n label.text = " Title "\n //\xe2\x80\xa6\n label.translatesAutoresizingMaskIntoConstraints = false //Must use\n return label\n}()\n\n//\xe2\x80\xa6\n\n\n//MARK: init\n//Add Subviews and then layout Contraints to the Cell\xe2\x80\x99s contentView\noverride init(style: UITableViewCellStyle, reuseIdentifier: String?) {\n super.init(style: style, reuseIdentifier: reuseIdentifier)\n addSubViewsAndlayout()\n}\n\n\n\n/// Add and sets up subviews with programmically added constraints\nfunc addSubViewsAndlayout() {\n contentView.addSubview(titleLabel) //will crash if not added\n\n let screenwidth = UIScreen.main.bounds.width //get any other properties you need\n\n titleLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 12.0).isActive = true\n titleLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true\n titleLabel.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 12).isActive = true\n titleLabel.rightAnchor.constraint(equalTo: otherViewToTheSide.rightAnchor, constant: -24).isActive = true \n\n//...\nRun Code Online (Sandbox Code Playgroud)\n\n我还喜欢实现以下方法,而不是使用硬编码值/字符串。
\n\n/// Get the Height of the cell\n/// use this in heightForRowAt indexPath\n/// - Returns: CGFloat\nclass func height() -> CGFloat {\n return 175\n}\n//CustomCell2.height()\n\n\n/// Get the string identifier for this class.\n///\n/// - Retruns: String\nclass var identifier: String {\n return NSStringFromClass(self).components(separatedBy: ".").last!\n}\n//use when registering cell:\nself.tableView.register(CustomCell2.self, forCellReuseIdentifier: CustomCell2.identifier)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13582 次 |
| 最近记录: |