简体   繁体   English

调整我的 UICollectionViewCell 大小的问题

[英]Issues with resizing my UICollectionViewCell

I am currently trying to resize my cell within my CollectionView.我目前正在尝试在我的 CollectionView 中调整单元格的大小。 I have already implemented the dataSource, CollectionViewDelegate and CollectionViewDelegateFlowLayout.我已经实现了 dataSource、CollectionViewDelegate 和 CollectionViewDelegateFlowLayout。

This is the code I have for the latter:这是我为后者提供的代码:

   extension ViewController: UICollectionViewDelegateFlowLayout {

  func collectionView(_ collectionView: UICollectionView, layout 
collectionViewLayout: UICollectionViewLayout, sizeForItemAt 
indexPath: IndexPath) -> CGSize
    {
     return CGSize(width: 100.0, height: 100.0)
    }
} 

The problem is that when I head over to my storyboard and select my CollectionView and then go to the Attributes Inspector to switch "Estimate Size" from "Automatic" to "None," I would get three columns after running my app [Image #1]. The problem is that when I head over to my storyboard and select my CollectionView and then go to the Attributes Inspector to switch "Estimate Size" from "Automatic" to "None," I would get three columns after running my app [Image #1 ]。

在此处输入图像描述

The result I am looking for is to be able to have one middle column with a bigger cell as I show on Image #2.我正在寻找的结果是能够拥有一个具有更大单元格的中间列,正如我在图像 #2 上显示的那样。

在此处输入图像描述

Has anybody dealt with this problem before?以前有人处理过这个问题吗?

In order to show a collection view cells as a list, you can change your cell width equals to the width of the collection view.为了将集合视图单元格显示为列表,您可以将单元格宽度更改为集合视图的宽度。 Giving constant width will not work for different device size.提供恒定宽度不适用于不同的设备尺寸。


  func collectionView(_ collectionView: UICollectionView, layout 
collectionViewLayout: UICollectionViewLayout, sizeForItemAt 
indexPath: IndexPath) -> CGSize
    {
       let width = collectionView.frame.width
     return CGSize(width: width, height: 100.0)
    }
}

And give your desired constraint values for the inner rectangular view to achieve the desired look.并为内部矩形视图提供所需的约束值以实现所需的外观。 Make collectionView cell background to clear.使 collectionView 单元格背景清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM