我一直在想,怎样才能让单元格填满宽度,正如你在图片中看到的,单元格之间的宽度太大了.我使用的自定义单元格只有一个imageView.

在此处输入图像描述

我试图从故事板上定制它,但我想没有选项,或者应该通过编程来完成.

my UICollectionViewController:

 @IBOutlet var collectionView2: UICollectionView!

    let recipeImages = ["angry_birds_cake", "creme_brelee", "egg_benedict", "full_breakfast", "green_tea", "ham_and_cheese_panini", "ham_and_egg_sandwich", "hamburger", "instant_noodle_with_egg.jpg", "japanese_noodle_with_pork", "mushroom_risotto", "noodle_with_bbq_pork", "starbucks_coffee", "thai_shrimp_cake", "vegetable_curry", "white_chocolate_donut"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Do any additional setup after loading the view.

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
         return recipeImages.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RecipeCollectionViewCell

        // Configure the cell
        cell.recipeImageView.image = UIImage(named: recipeImages[indexPath.row])

        return cell
    }

推荐答案

你需要按程序来做.

在视图控制器中实现UICollectionViewDelegateFlowLayout,并提供collectionView:layout:sizeForItemAtIndexPath:中的大小

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let kWhateverHeightYouWant = 100
        return CGSizeMake(collectionView.bounds.size.width, CGFloat(kWhateverHeightYouWant))
}

您还需要在视图控制器的viewWillLayoutSubviews()中调用collectionView.collectionViewLayout.invalidateLayout(),这样当主视图的尺寸发生变化时(例如,旋转时),集合视图将重新布局.

Swift 4 Update

func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
            let kWhateverHeightYouWant = 100
            return CGSize(width: collectionView.bounds.size.width, height: CGFloat(kWhateverHeightYouWant))
}

Swift相关问答推荐

RealityKit(VisionOS)中的PhysicBodyComponent和DragGesture

我可以为UIMenu设定一个固定的订单吗?

';NSInternal不一致异常';,原因:';可见导航栏Xcode 15.0 Crash请求布局

SwiftUI 拖放:如果没有上下文类型,则无法解析对成员‘first’的引用

ToolbarItem 包含在动画中但不应该包含在动画中

如何使一个 Reality Composer 场景中的分组对象可拖动?

SwiftUI 无法在实时活动中显示 SpriteView

如何从 RC 加载场景作为 ModelEntity 而不是普通实体?

Firebase removeObserver 在 Swift 5 中不起作用

在主要参与者中启动分离任务和调用非隔离函数之间的区别

如何在 SwiftUI 中获取视图的位置

在 Swift 中,如何查看 Process() 传递给 shell 的字符串?

在 Swift 中从 HTML 生成带有图像的 PDF 而不显示打印界面

iOS 判断应用程序是否可以访问麦克风

如何快速识别字符串中的大写和小写字符?

如何发送静默推送通知有效负载

WKWebView 确实从本地文档文件夹加载资源

如何在 Swift 中判断变量的类型

Void 函数中意外的非 Void 返回值 (Swift 2.0)

Swift 2.0 按属性对对象数组进行排序