【问题标题】:Getting constraint error on updating the constraint in cellforRow在 cellforRow 中更新约束时出现约束错误
【发布时间】:2018-04-16 07:21:53
【问题描述】:

我有一个 UITableViewCell,里面有 collectionView。约束是非常简单的前导 0 ,尾随 0 ,前 10 和后 10 并且 UICollection 视图的高度约束是 160 。将 IBOutlet 连接到高度约束。在行的单元格中,我正在更新高度约束。

但在调试时出现错误

行的单元格代码是

func videoCell(for tableView:UITableView ,with indexPath:IndexPath , videos:[VideoCollectionViewCellConfigure] ) -> VideoTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "VideoTableViewCell", for: indexPath) as! VideoTableViewCell
    cell.videoConfigurators = videos
    cell.collectionCellClickedBlock = self.videoCollectionCellClicked(_:_:)

    if videos.count > 0 {
        let video = videos[0]
        cell.videoCollectionViewHeightConstraint.constant = video.height
    }

    cell.videoCollectionView.reloadData()
    return cell
}




    2018-04-16 12:42:10.815998+0530 CineBee[5280:74417] [LayoutConstraints] 
    Unable to simultaneously satisfy constraints.
 Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
                (2) find the code that added the unwanted constraint or 
constraints and fix it. 
    (
"<NSLayoutConstraint:0x6000006878a0 UICollectionView:0x7f86109c4c00.height 
   == 180   (active)>",
"<NSLayoutConstraint:0x604000885870 V:[UICollectionView:0x7f86109c4c00]-(10)-|   (active, names: '|':UITableViewCellContentView:0x7f8615803ca0 )>",
"<NSLayoutConstraint:0x604000888de0 V:|-(10)- 
   [UICollectionView:0x7f86109c4c00]   (active, names: '|':UITableViewCellContentView:0x7f8615803ca0 )>",
"<NSLayoutConstraint:0x60400088dde0 'UIView-Encapsulated-Layout-Height' 
    UITableViewCellContentView:0x7f8615803ca0.height == 180.5   (active)>"
 )

  Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x6000006878a0 
  UICollectionView:0x7f86109c4c00.height == 180   (active)>

【问题讨论】:

  • 显示整个错误日志.. 它告诉您哪些约束存在冲突
  • 也可以试试wtfautolayout.com
  • 此外,解释一下如何定义 tableView 单元格的高度。它是静态高度,还是计算的自动布局?显示相关代码
  • 你应该调整单元格的大小来保存视频,调整单元格的大小使用heightForRowAtIndexPath
  • 在 HeightForRow 我正在返回 UITableViewAutomaticDimension。

标签: ios uitableview autolayout


【解决方案1】:

试试这个

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var defaultHeight = /*your default height*/
    if videos.count > 0 {
        let video = videos[0]
        return video.height + 10 + 10 // video height + top padding + bottom padding 
    }

    return defaultHeight
}

同时从界面生成器中移除collectionView 高度约束。现在您不必在cellForItemAtIndexPath 中设置约束。

【讨论】:

    【解决方案2】:

    根据您的错误报告,这似乎是我试图在此SO question 中解决的情况。如果冲突约束中包含UIView-Encapsulated-Layout-Height,则表示在基于自动布局计算动态高度时发生冲突。只需将垂直约束优先级之一降低到999 - 如果代码有效,那就没问题了,你可以开始了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多