微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何设置集合单元视图以动态拉伸到 iphone屏幕宽度(例如 iphone 5s,iphone 6 plus)?

我试过了:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    (ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass
                                                                      forIndexPath:indexPath];

    cell.bounds = CGRectMake(0,self.view.bounds.size.width,150);

    return cell;
}

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

我试过添加这个委托方法

- (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);
}

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

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

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

原文地址:https://www.jb51.cc/iOS/334845.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐