ios - collectionViewLayout sizeForItemAtIndexPath -> CGSize 奇怪的行为

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

所以很有趣,我正在尝试使用 collectionViewLayout sizeForItemAtIndexPathcollectionView 中一次只显示一个项目并水平滚动,我试过这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0.0;
    
    flowLayout.minimumLineSpacing = 0.0;
    
    _obj_CollectionView.pagingEnabled = YES;
    _obj_CollectionView.collectionViewLayout = flowLayout;
    
    self.obj_CollectionView.delegate = self;
    self.obj_CollectionView.dataSource = self;
    _obj_CollectionView.backgroundColor = [UIColor redColor];
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height);
}

这是 collectionView 截图:

enter image description here

这里紫色是我的单元格,红色是我的 collectionView 但我想要整个紫色。

所以请指导我。我很迷惑。 :)

最佳答案

首先,您不需要以编程方式创建UICollectionViewFlowLayout 的对象。也不需要 viewDidLoad() 中编写的代码。您可以使用 storyboarddelegate 方法完成所有这些操作。

下面是你的问题的代码:

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
}

extension ViewController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.purple : UIColor.blue
        return cell
    }
}

extension ViewController : UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

Storyboard截图:

enter image description here

输出屏幕:

enter image description here

关于ios - collectionViewLayout sizeForItemAtIndexPath -> CGSize 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653789/

相关文章:

objective-c - NSArrayController 上的 SelectionIndexes 只返回一个值

objective-c - 我如何查看哪些方法正在减慢我的 UIViewController 的显示速度?

ios - 强制 UICollectionView 停止滚动

ios - 表/ Collection View 的水平滚动

c# - Xamarin.iOS 如何在没有导航栏时更改状态栏颜色

iOS 和 CoreAudio : Calling MusicPlayerSetTime() stops MusicPlayer

ios - 如何知道 iOS 中是否显示了位置服务对话框?

iphone - 如何在 iOS 中插入和覆盖音频文件

ios - 如何在 UICollectionView 中添加 Load more cell?

iphone - 在有限的时间内锁定 iPhone