我在UICollectionViewCell中使用自动布局和大小类。因此,想法是使UI与Instagram相似。
问题是我无法完全理解如何添加适当的自动布局,因为似乎添加缺少的约束并不能神奇地完成这些任务。
当我添加约束时,即使没有警告,它也会始终在目标输出上显示“将尝试通过打破约束来进行恢复”。目标是使配置文件单元根据iphone类型动态变化,并根据bioLabel使动态高度变化。现在,我将其设置为静态,这是非常错误的,因为使用此方法时
systemLayoutSizeFittingSize:UILayoutFittingCompressedSize
它总是返回零,我猜是因为放错了自动布局约束。您能建议我如何根据其约束条件计算单元格的大小吗?我在github中创建了该项目,非常感谢任何帮助!
我下载了您的代码并发现了问题。瞧,您可以简单地通过替换collectionView:layout:sizeForItemAtIndexPath方法中的这一行代码来解决您的问题。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)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)findHeightForText:(NSString *)text havingMaximumWidth:(CGFloat)widthValue andFont:(UIFont *)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;
}
更换一次并在所有设备上测试,让我知道...希望对您有所帮助:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句