如何将集合单元格 View 设置为动态拉伸(stretch)到 iphone 屏幕宽度(例如 iphone 5s、iphone 6 plus)?
我试过:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
(ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass
forIndexPath:indexPath];
cell.bounds = CGRectMake(0,0, self.view.bounds.size.width, 150);
return cell;
}
那是行不通的。我没有看到内容拉伸(stretch)到屏幕的右侧。
我试过添加这个委托(delegate)方法:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize;
cellSize.width = self.view.bounds.size.width;
// body view height
cellSize.height = 150;
return cellSize;
}
我在方法中设置了断点,但该方法从未被调用?
最佳答案
我遇到了相同用例的相同问题。我终于结束了
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.bounds.size.width, 150);
}
这会更改 Collection View 中每个项目(单元格)的大小。因此,您可以使用它更改每个单元格的大小。在这里我需要单元格宽度与我的 UICollectionView 相同,所以我传递了我想要的 UICollectionview 的宽度和特定高度。希望这会有所帮助。
也不需要在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
中设置单元格边界,因为大小是通过 - 设置的(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
还要确保您已将所需的 Delegates & DataSources
附加到 UICollectionView
关于ios - 如何将集合单元格宽度设置为动态拉伸(stretch)到手机宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117449/