简体   繁体   English

如何使用 Collection View 创建 2 x 2 网格布局?

[英]How to create 2 x 2 grid layout using Collection View?

I am trying to create 2x2 grid layout using collection view.我正在尝试使用集合视图创建 2x2 网格布局。

I have used below code.我使用了下面的代码。 It's kind of working fine for iPhone 5 but with tweaks.它在 iPhone 5 上运行良好,但需要进行一些调整。 I am trying to write code that can be used on all screen sizes.我正在尝试编写可用于所有屏幕尺寸的代码。 This is not working on iPhone 6.这不适用于 iPhone 6。

public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if let cell1 = cell as? CollectionViewCell {
            return cell1
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

        let padding: CGFloat =  20
        let collectionViewSize = collectionView.frame.size.width - padding

        return CGSize(width: collectionViewSize/2, height: collectionViewSize/2+15)
    }

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

    public func collectionView(
        _ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }

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

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }

I am trying to achieve following layout:我正在尝试实现以下布局:预期的

In future this grid can be like 2 columns only and N rows.将来这个网格可能只有 2 列和 N 行。 Each row will always have 2 items.每行将始终有 2 个项目。

Use:利用:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

        let padding: CGFloat =  20
        let size = self.view.frame.width - padding

        return CGSize(width: size/2, height: size/2+15)
    }

First of all, get three variables together depending on your needs.首先,根据您的需要将三个变量放在一起。 Those are the minimum spacing (Both InteritemSpacing and Line spacing - Should be same UXwise but you can change that in delegate methods if you like. The interitem space however must always be equal to minimumSpacing ), edgeInsetPadding and the number of items you want in a row.这些是最小间距(InteritemSpacing 和行间距 - 应该是相同的 UXwise,但如果您愿意,您可以在委托方法中更改它。但是,interitem 空间必须始终等于minimumSpacing )、edgeInsetPadding 和您想要的项目数排。

private var numberOfItemsInRow = 2

private var minimumSpacing = 5

private var edgeInsetPadding = 10

In your code, you have already defined your Edge Insets as:在您的代码中,您已经将 Edge Insets 定义为:

 UIEdgeInsets(
            top: 5, left: 5, bottom: 5, right: 5
        )

The left and right insets are important for correctly determining the size of the item.左右插图对于正确确定项目的大小很重要。 left+right gives us a grand sum of 10. This should be assigned to edgeInsetPadding like this: left+right 给了我们一个 10 的总和。这应该像这样分配给edgeInsetPadding

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let inset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        edgeInsetPadding = inset.left+inset.right
        return inset
    }

Now lets get to your UICollectionViewDelegateFlowLayout .现在让我们来看看你的UICollectionViewDelegateFlowLayout Update the following methods.更新以下方法。

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

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

Now lets get to the main part.现在让我们进入主要部分。 Modify your sizeForItemAt in UICollectionViewDelegateFlowLayout as:sizeForItemAt中的UICollectionViewDelegateFlowLayout修改为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (Int(UIScreen.main.bounds.size.width) - (numberOfItemsInRow - 1) * minimumSpacing - edgeInsetPadding) / numberOfItemsInRow
        return CGSize(width: width, height: width)
    }

And this is it.就是这样。 You now get two equal size tiles in 2x2 grid.您现在在 2x2 网格中获得两个相同大小的图块。 If you want to change this in future, just change the numberOfItemsInRow variable to something else.如果您想在将来更改它,只需将numberOfItemsInRow变量更改为其他值。 Like 3 for 3x3.就像 3 对 3x3。

This will be done using the item size of the collectionViewCell这将使用collectionViewCell的项目大小来完成

collectionView.delegate = self 
extension ViewController : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let padding = 5
        let width = (collectionView.frame.size.width - CGFloat(padding) * 2) / CGFloat(2)
        let height = width / 200 * 110 // or what height you want to do
        return CGSize(width: width, height: height)
    }
}

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

相关问题 如何使用集合视图单元格Swift iOS实现网格视图 - How to achieve Grid view using collection view cell swift ios 如何使用UIImageView和UICollectionView创建布局,并且集合视图在滚动时更改高度? - How to create Layout with UIImageView and UICollectionView and collection view changes height on scroll? 如何在使用“layoutAttributesForElements”时为集合视图布局更改设置动画? - How to animate collection view layout change while using `layoutAttributesForElements`? "如何使用集合视图组合布局使集合视图内的单元格居中" - How to center cells inside collection view using collection view compositional layout 如何使用 UICollectionViewCompositionalLayout 创建具有相等行高的网格布局 - How to create a grid layout with equal row heights using UICollectionViewCompositionalLayout 使用自动布局的具有动态高度的网格视图 - Grid view with dynamic height using auto layout 如何布置单行集合视图? - How to layout single row collection view? 如何清除集合视图布局iOS上的内容 - How to clear content on a collection view layout ios 如何实现这种类型的集合视图网格 - How to achieve this type of collection view grid 使用ios中的集合视图创建视图 - Create a view using collection view in ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM