我的锚有什么问题?

时间:2017-03-24 19:31:58

标签: swift

我的Swift代码中有这个锚点

    tableView.anchor(PostsDiv.bottomAnchor, left:view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 40, leftConstant: 15, bottomConstant: 0, rightConstant: 15, widthConstant:0,heightConstant:tableView.contentSize.height+tableView.contentInset.bottom+tableView.contentInset.top) 
    ProfilePicture.anchor(cell.topAnchor, left:cell.leftAnchor, bottom: nil, right: nil, topConstant: 15, leftConstant: 14, bottomConstant: 0, rightConstant: 0, widthConstant: 50,heightConstant:50)
    username.anchor(cell.topAnchor, left:ProfilePicture.rightAnchor, bottom: postText.topAnchor, right: cell.rightAnchor, topConstant: 20, leftConstant: 13, bottomConstant: 0, rightConstant: 0)
    postText.anchor(nil, left:ProfilePicture.rightAnchor, bottom:border.topAnchor, right: cell.rightAnchor, topConstant: 0, leftConstant: 15, bottomConstant: 20, rightConstant: 0)
    border.anchor(nil, left:cell.leftAnchor, bottom: cell.bottomAnchor, right: cell.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 20, rightConstant: 0,heightConstant:1)

但是我得到了这个错误,我的约束是错误的 `

"<NSLayoutConstraint:0x608000286540 V:|-(20)-[UILabel:0x7f941ac2a200'theChampagnepapi']   (active, names: '|':UITableViewCell:0x7f941b016600 )>",
    "<NSLayoutConstraint:0x6080002865e0 UILabel:0x7f941ac2a200'theChampagnepapi'.bottom == UITextView:0x7f941b0b2800'He is right'.top   (active)>",
    "<NSLayoutConstraint:0x608000286720 UITextView:0x7f941b0b2800'He is right'.bottom == UIView:0x7f941ac2b190.top - 20   (active)>",
    "<NSLayoutConstraint:0x608000286860 UIView:0x7f941ac2b190.bottom == UITableViewCell:0x7f941b016600.bottom - 20   (active)>",
    "<NSLayoutConstraint:0x608000286b80 'UIView-Encapsulated-Layout-Height' UITableViewCell:0x7f941b016600.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000286860 UIView:0x7f941ac2b190.bottom == UITableViewCell:0x7f941b016600.bottom - 20   (active)>

我的约束有什么问题?

1 个答案:

答案 0 :(得分:0)

问题是UIView-Encapsulated-Layout-Height(你没有设置,而是表格视图)。所以,有两件事:

  1. viewDidLoad中指定UITableViewAutomaticDimension,以确保可以根据单元格内的约束调整表格的行数:

    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension
    
  2. 我发现如果约束条件的行高可能不同,我有时必须将我的一个垂直约束的priority减少到小于1000.即使999就足够了。我通常只对其中一个约束(例如底部约束)执行此操作。