【发布时间】:2015-02-27 09:06:30
【问题描述】:
我正在尝试在 UICollectionReusableView (UICollectionView Header) 上下文中进行这项工作。
UILabels A、B 和 C 中的字符串所需的高度被计算并设置为灰色视图的高度,有点像这样:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
....
CGFloat expectedHeightForLabelB = 0;
if (stringB.length > 0)
{
expectedHeightForLabelB = 8 + calculatedRequiredHeightForStringB;
}
....
CGFloat expectedHeaderHeight = expectedHeightForLabelA + expectedHeightForLabelB + expectedHeightForLabelC + 8
return CGSizeMake(collectionView.bounds.size.width, expectedHeaderHeight);
}
在
collectionView:viewForSupplementaryElementOfKind:atIndexPath:,我有一个条件
if (stringB.length == 0)
[headerView.labelB removeFromSuperView];
在这种情况下,我已经解决了灰色超级视图的估计动态高度。我希望 UILabel A 和 C 有一个 8 pts 的后备垂直约束。
我只能想到 2 种可能的(不确定是对还是错)方法来实现这一点。
- 以编程方式在
collectionView:viewForSupplementaryElementOfKind:atIndexPath:的代码中应用新的后备布局。 - 在情节提要中设置具有常量 8 和更低优先级的约束。
实现这一目标的正确或最佳方法是什么?我已经看到了here 的答案,但如果有的话,我想收集更强大的输入。
提前致谢!
【问题讨论】:
标签: ios uiview autolayout nslayoutconstraint