從代碼你發佈,你只是設置標題的大小,但我想你也有類似
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
,也許你需要檢查你有沒有約束,對於分離器例如。
你是否對自定義集合視圖單元格中的子視圖應用了約束? –
你在哪裏設置單元格的寬度?你使用collectionViewFlowLayout委託方法或其他東西? –
@NileshMahajan是的。對於單元格和部分(這是一個自定義可重用的視圖) – vyudi