UIView-Encapsulated-Layout-Width/Height限制为零 [英] UIView-Encapsulated-Layout-Width / Height Constrained to Zero

查看:395
本文介绍了UIView-Encapsulated-Layout-Width/Height限制为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于UITableView单元格和节页眉/页脚的自动布局,存在许多堆栈溢出问题,这些问题涉及UIView-Encapsulated-Layout-Width约束,UITableView用于使其组件具有正确的大小:

这些约束的目的似乎很明确–它们允许UITableViewUICollectionView与约束布局进行交互,并传递其有关应该有多大单元格(和其他组件)的规范.

大多数问题似乎是人们具有一组必需约束,这些约束与这些封装约束不兼容,并且也需要大小为0.最可行的答案似乎是将用户定义的约束的优先级降低至999,以便框架可以推翻这些约束.

但是–我感兴趣的理解是为什么 UITableView要求其中一个视图的宽度为0? 这是一个错误吗?还是不正确使用的结果?还是说得通?

如果将单元格从零展开,则 0的高度限制有时会很有意义.降低优先级是解决此问题的好方法.但是,为什么单元格的宽度0?

?

解决方案

UIView没有固有的大小,因此需要宽度和高度限制. UIView-Encapsulated-Layout-Width UIView-Encapsulated-Layout-Height 最初是由UICollectionView/UITableView创建的.然后,根据sizeForItem或heightForRowAtIndexPath委托方法设置约束常量.为了确保在cellForItem/cellForRow方法中创建的UICollectionViewCell/UITableViewCell具有确切的大小.

There are a number of stack overflow questions about auto-layout of UITableView cells and section headers / footers that concern the UIView-Encapsulated-Layout-Width constraints that the UITableView uses to make its components be the correct size:

The purpose of these constraints seems pretty clear – they allow the UITableView and also UICollectionView to interface with constraint layout and pass in its specification about how large cells (and other components) should be.

Most issues seem to be that people have a set of required constraints that are incompatible with these encapsulation constraints also requiring a size of 0. The most workable answers seem to be to lower the priority of user defined constraints to 999 so that the framework's can overrule them.

However – what I'm interested in understanding is why is UITableView requiring a width of 0 for one of its views? Is this a bug? Or is it a results of incorrect use? Or does it make perfect sense?

Height constraints of 0 sometimes make sense, if cells are being expanded from nothing. Lowering priorities is a good way to handle this. But why would the cells be getting a width of 0?

解决方案

UIView doesn't have the intrinsic size hence width and height constraints needed. UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height are created by UICollectionView / UITableView initially. And later on sets the constraint constant based to the sizeForItem or heightForRowAtIndexPath delegate method. To ensure that UICollectionViewCell / UITableViewCell created in cellForItem / cellForRow method will be having the exact size.

这篇关于UIView-Encapsulated-Layout-Width/Height限制为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆