我正在使用 UICollectionViewCell 的自动布局和大小类。所以我们的想法是制作一个与 Instagram 相似的 UI。
问题是我无法完全理解如何添加正确的自动布局,因为似乎添加缺少的约束并没有神奇地做到这一点。
当我添加约束时,即使没有警告,它总是在目标输出上显示“将尝试通过打破约束来恢复”。目标是根据 iphone 类型使配置文件单元动态,并根据 bioLabel 动态高度。现在我把它设为静态,这是非常错误的,因为当我使用这种方法时
systemLayoutSizeFittingSize:UILayoutFittingCompressedSize
它总是返回零,我猜是因为自动布局约束放错了位置。你能告诉我如何根据它的约束计算单元格的大小吗?我已经在 github 上创建了这个项目,任何帮助将不胜感激!
CollectionViewAutoLayout
Best Answer-推荐答案 strong>
我下载了您的代码并发现了问题。看,您只需在 collectionView:layout:sizeForItemAtIndexPath 方法中替换这行代码即可解决您的问题。
- (CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPathNSIndexPath *)indexPath {
if (indexPath.section == 0) {
CGFloat height = [self findHeightForText"Description about yourself, who you are and what impact did you do to the environment. did you do to the environment. Description about yourself, who you are and what impact did you do to the environment. did you do to the environment. who you are and what impact did you do to the environment. did you do to the environment. who you are and what impact did you do to the environment. did you do to the environment." havingMaximumWidth:[UIScreen mainScreen].bounds.size.width andFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
return CGSizeMake([UIScreen mainScreen].bounds.size.width, 150 + height);
}
else {
return CGSizeMake((([UIScreen mainScreen].bounds.size.width - 2.0) / 3.0), (([UIScreen mainScreen].bounds.size.width - 4.0) / 3.0));
}
}
添加此方法动态计算高度。
#pragma mark - Height Calculation Method
- (CGFloat)findHeightForTextNSString *)text havingMaximumWidthCGFloat)widthValue andFontUIFont *)font {
CGSize size = CGSizeZero;
if (text) {
//iOS 7
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height + 1);
}
return size.height;
}
更换一次并在所有设备上进行测试,然后告诉我...希望对您有所帮助
关于ios - 基于UILabel的UICollectionViewCell AutoLayout动态高度,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29933364/
|