ios – 如何将集合单元格宽度设置为动态拉伸到手机宽度

前端之家收集整理的这篇文章主要介绍了ios – 如何将集合单元格宽度设置为动态拉伸到手机宽度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何设置集合单元视图以动态拉伸到 iphone屏幕宽度(例如 iphone 5s,iphone 6 plus)?

我试过了:

  1. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
  2. cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  3.  
  4. (ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass
  5. forIndexPath:indexPath];
  6.  
  7. cell.bounds = CGRectMake(0,self.view.bounds.size.width,150);
  8.  
  9. return cell;
  10. }

这不起作用.我没有看到内容延伸到屏幕的右侧.

我试过添加这个委托方法

  1. - (CGSize)collectionView:(UICollectionView *)collectionView
  2. layout:(UICollectionViewLayout *)collectionViewLayout
  3. sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  4. {
  5. CGSize cellSize;
  6.  
  7. cellSize.width = self.view.bounds.size.width;
  8.  
  9. // body view height
  10. cellSize.height = 150;
  11.  
  12. return cellSize;
  13. }

我在方法中设置了断点,但该方法永远不会被调用

解决方法

我对同一个用例有同样的问题.我终于结束了
  1. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. return CGSizeMake(collectionView.bounds.size.width,150);
  4. }

这会更改集合视图的每个项目(单元格)的大小.因此,您可以使用此更改每个单元格的大小.在这里,我需要单元格宽度与我的UICollectionView相同,所以我传递了UICollectionview的宽度和我想要的特定高度.希望这会有所帮助.

此外,没有必要在 – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中设置单元格边界,因为大小通过 – (CGSize)collectionView设置:(UICollectionView *)collectionView布局:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

还要确保您已连接所需的代表和DataSources到UICollectionView

猜你在找的iOS相关文章