简体   繁体   English

UICollectionView作为子视图不会更新约束

[英]UICollectionView as a subview doesn't update constraints

I'm having an autolayout puzzle. 我有一个自动布局难题。 Here's the description: 描述如下:

I have a UITableViewCell that has a "container view" which is a basic UIView and then UICollectionView is a subview of this container view. 我有一个UITableViewCell ,它具有一个“容器视图”,它是一个基本的UIView ,然后UICollectionView是此容器视图的子视图。

UITableViewCell -> UIView -> UICollectionView

It's a bit simplified from the end goal which includes a little more views under the tableViewCell, but for the sake of the question it should be good enough. 与最终目标相比有所简化,最终目标包括在tableViewCell下的更多视图,但出于问题的考虑,它应该足够好。

Both UIView and UICollectionView have constraints in the following format: UIViewUICollectionView都具有以下格式的约束:

H:|[view]| and V:|[view]| (eg take full size of the tableViewCell). (例如,使用tableViewCell的完整大小)。

For some reasons the constraints are not being updated after UICollectionView lays out it's cells. 由于某些原因,在UICollectionView对单元格进行布局后,约束不会更新。

Here's what works: 这是有效的方法:

  1. If I replace UICollectionView with a UILabel , then it works fine (eg tableViewCell is properly dynamically sized depending on the label text). 如果我将UICollectionView替换为UILabel ,那么它可以正常工作(例如,根据标签文本正确调整tableViewCell的大小)。

  2. If I remove the intermediate UIView (the container view) from the puzzle then again it all works fine (eg tableViewCell is properly dynamically sized depending on number of cells in UICollectionView). 如果我从拼图中删除了中间的UIView(容器视图),那么它又可以正常工作(例如,根据UICollectionView中的单元格数目,可以适当地动态调整tableViewCell的大小)。

What doesn't work is when I have UICollectionView added to the intermediate view like this: UITableViewCell -> UIView -> UICollectionView 什么是行不通的 ,当我这样将UICollectionView添加到中间视图时: UITableViewCell -> UIView -> UICollectionView

Any ideas how to solve this? 任何想法如何解决这个问题?

Apple does not recommend use as subviews other scrollable objects inside uitableview. Apple不建议将uitableview中的其他可滚动对象用作子视图。

To achieve your goal: 为了实现您的目标:

  1. Add equalWidth constaint for your UICollectionView (to uitableviewcell) 为您的UICollectionView添加equalWidth约束(到uitableviewcell)
  2. Add equalHeight constaint for your UICollectionView (to uitableviewcell) 为您的UICollectionView添加equalHeight约束(到uitableviewcell)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM