基于 UILabel 的 UICollectionViewCell AutoLayout 动态高度
UICollectionViewCell AutoLayout dynamic height based on UILabel
我在 UICollectionViewCell 中使用自动布局和大小 类。所以想法是做一个与Instagram类似的UI。
问题是我无法完全理解如何添加正确的自动布局,因为添加缺少的约束似乎并没有神奇地完成这些事情。
当我添加约束时,即使没有警告,它总是在目标输出上显示 "Will attempt to recover by breaking constraint"。目标是根据 iphone 类型使配置文件单元格动态化,并根据 bioLabel 使动态高度。现在我把它设为静态,这是非常错误的,因为当我使用这种方法时
systemLayoutSizeFittingSize:UILayoutFittingCompressedSize
它总是 return 零,我猜这是因为错位的自动布局约束。你能告诉我如何根据它的约束计算单元格的大小吗?我在 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;
}
更换一次并在所有设备中进行测试,然后告诉我...希望对您有所帮助:)
我在 UICollectionViewCell 中使用自动布局和大小 类。所以想法是做一个与Instagram类似的UI。
问题是我无法完全理解如何添加正确的自动布局,因为添加缺少的约束似乎并没有神奇地完成这些事情。
当我添加约束时,即使没有警告,它总是在目标输出上显示 "Will attempt to recover by breaking constraint"。目标是根据 iphone 类型使配置文件单元格动态化,并根据 bioLabel 使动态高度。现在我把它设为静态,这是非常错误的,因为当我使用这种方法时
systemLayoutSizeFittingSize:UILayoutFittingCompressedSize
它总是 return 零,我猜这是因为错位的自动布局约束。你能告诉我如何根据它的约束计算单元格的大小吗?我在 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;
}
更换一次并在所有设备中进行测试,然后告诉我...希望对您有所帮助:)