UICollectionView内容没有正确调整大小
我在静态表格视图单元格内使用集合视图,我使用自动布局来定义集合视图的框架。另外,我使用FlowLayout作为collectionViewLayout。UICollectionView内容没有正确调整大小
问题:
- 的行为就像预期的iPhone 4S和5的屏幕。 (宽度相同)
- 一旦宽度发生变化(iPhone 6和6 Plus),集合视图会正确调整大小,但内容会保持其大小。
- 这对于单元格和部分都会发生。
额外的信息:
我设置
collectionViewLayout minimumInteritemSpacingForSectionAtIndex
为零。细胞的宽度如果我配置的布局总是被设置为
tableView.frame.size.width/2
-
:
CGFloat headerHeight = 52; collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); collectionViewLayout.headerReferenceSize = CGSizeMake(self.width, headerHeight);
这是它现在看起来。绿色部分代表UICollectionView。黑色和蓝色的细胞。分界线应该调整大小,单元格应该分成2列。
从代码你发布,你只是设置标题的大小,但我想你也有类似
collectionViewLayout.itemSize = CGSizeMake(self.collectionView.frame.width/2, height);
的重要组成部分,是在该方法中配置这一点,因为如果你在viewDidLoad中做到这一点的看法是不正确layouted却又如此的宽度也只是在故事板的一组,无论在哪个设备你运行你的应用程序。
的解决办法是将上面的代码中viewWillLayoutSubviews
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
collectionViewLayout.itemSize = CGSizeMake(self.collectionView.frame.width/2, height);
collectionViewLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.width/2, height);
}
另一种方式是实现的FlowLayout委托方法:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return cCGSizeMake(collectionView.frame.width/2, CUSTOM_HEIGHT);
}
// The same for the header
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return cCGSizeMake(collectionView.frame.width/2, CUSTOM_HEIGHT);
}
请务必设置委托的collectionView
让我知道它是怎么回事:)
更新
对于细胞,添加这些方法太:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return CGRectZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
对于sectionHeader
,也许你需要检查你有没有约束,对于分离器例如。
一定要使用所有用于精确造型如下:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
更改单元格内容的大小,使用:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((kScreenWidth/2-8), 44); // Use your own X and Y sizes
}
感谢您的关注。但这不是我的答案。内容保持原始大小。另外,我已经在实施单元格内容大小。 – vyudi
你是否对自定义集合视图单元格中的子视图应用了约束? –
你在哪里设置单元格的宽度?你使用collectionViewFlowLayout委托方法或其他东西? –
@NileshMahajan是的。对于单元格和部分(这是一个自定义可重用的视图) – vyudi