我有一个集合视图固定到它的超级视图的底部,左侧和右侧边缘,并且文本字段固定到超级顶部,左侧和右侧。 updateConstraints
从下到上增加了集合视图的高度,其中基于其内容大小的高度约束随着更多项目的添加而增加。使用不等式约束和优先级限制UICollectionView高度
我希望集合视图高度增长但不超过 上方的textField的底部,并在 集合视图的顶部边缘有一个不等式约束。以下是我已经试过:
self.topResistanceConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.textField
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
[self addConstraint:self.topResistanceConstraint];
CGSize size = self.collectionView.collectionViewLayout.collectionViewContentSize;
self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:size.height];
[self addConstraint:self.heightConstraint];
这将导致不可满足的约束,并恢复(实际上似乎工作),但它不是一个很好的解决方案,如果布局需要适应键盘不起作用出现或消失。任何建议如何创建约束或优先级,以使其在不受约束的情况下工作?
大小已经用self.collectionView.collectionViewLayout.collectionViewContentSize计算出来了。我想完成的是消除模糊布局,同时使顶部约束优先于高度约束。 – user1192805