我有一个带有 2 种不同类型的 UICollectionViewCells 的 UICollectionView。有一个选项卡提示显示每种类型的单元格,并且一次只能显示一种类型的单元格。
由于某种原因,一种类型的单元格在 UICollectionView 中向左对齐,但另一种单元格在中心水平对齐。我希望两种类型的单元格左对齐。
边缘插入是:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10,10,10,10);
}
任何帮助将不胜感激。
最佳答案
单元格的布局会根据其大小而有所不同,因此您需要为每种单元格类型使用不同的插图。我认为应该是这样的:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section];
if ([cell isKindOfClass:[MyClass1 class]]){
return UIEdgeInsetsMake(10,10,10,10);
}else{
return UIEdgeInsetsMake(20,10,10,10);
}
用正确的类名替换 MyClass1 并尝试边缘插入,直到获得所需的行为。
关于ios - UICollectionView 单元格对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946048/