【发布时间】:2018-05-01 13:17:40
【问题描述】:
我将UICollectionView 拉伸到整个屏幕宽度。我的UICollectionViewCells 是从MyViewCell.xib 加载的。我通过以下方式设置其大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5, CGRectGetHeight(collectionView.frame));
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 16;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
return cell;
}
在主视图控制器加载期间,我有:
[self.collView registerNib:[UINib nibWithNibName:@"MyViewCell" bundle:nil] forCellWithReuseIdentifier:@"myCell"];
self.collView.delegate = self;
self.collView.dataSource = self;
这使“单元格视图”的大小正确,但从 XIB 加载的内容仍然“小”且未拉伸。这意味着,我可以在屏幕上看到两个项目,但它们不是 strechtech,仅向左对齐并且具有大约 50 的固定宽度。
如果我在 iPhone 上使用相同的代码,它可以正常工作,问题只出在 iPad 上。两台设备的 iOS 版本相同 (10.3.3)。
【问题讨论】:
-
你能记录下你从
layoutSubviews()方法得到的frame大小,看看你的单元格大小是否正确... -
调用似乎是正确的。如果我记录 collectionView.frame 的大小,即整个屏幕和正确的高度,则在约束中设置。
-
在单元格本身中记录您从
layoutSubviews()获得的信息 -
框架也是正确的,例如。与我在 sizeForItemAtIndexPath 中设置的相同
-
你使用自动布局吗?
标签: ios objective-c uicollectionview uicollectionviewcell