简体   繁体   English

collectionView中的约束

[英]Constraints in collectionView

I want to have dynamic width and height in my collectionView cells so I have only 2 cells in every screen. 我希望在collectionView单元格中具有动态的宽度和高度,因此每个屏幕上只有2个单元格。 And 10px from top, left, right and between them. 距顶部,左侧,右侧及其之间的距离为10px。

Example: 例:

If I have 320px width(screen) I want 145px every cell so it would be 10px from left +145(cell) + 10px between + 145(2nd cell) + 10px from right. 如果我有320像素的宽度(屏幕),我希望每个单元格为145像素,那么它应该是左侧的10像素+145(单元格)+ 10像素之间的宽度+ 145(第二个单元格)+右侧的10像素。

Conform your controller to UICollectionViewDelegateFlowLayout protocol and implement the methods: 使您的控制器符合UICollectionViewDelegateFlowLayout协议并实现以下方法:

import UIKit

class ViewController: UIViewController
{
    let numberOfCells = 9
    let kCellHeight : CGFloat = 100
    let kLineSpacing : CGFloat = 10
    let kInset : CGFloat = 10

    @IBOutlet weak var collectionView: UICollectionView!

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

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sampleCell", for: indexPath)
        return cell
    }
}

extension ViewController : UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: (UIScreen.main.bounds.width - 2*kInset - kLineSpacing)/2, height: kCellHeight)
    }

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

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

Here is the attached screenshot: 这是所附的屏幕截图:

在此处输入图片说明

You either use stackviews or you can set width constraint of your cell and drag it into code to set the constant to half screen size. 您可以使用堆栈视图,也可以设置单元格的宽度约束并将其拖动到代码中以将常量设置为屏幕大小的一半。

Also why you're using constraints directly on cells when you have: 另外,为什么在以下情况下直接在单元格上使用约束:

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
    return CGSizeMake(width ,height);
}

Use the below code : 使用以下代码:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let width = (screenSize.width - 30) / 2 let height = width * aspectRatio // aspectRatio - someValue or put height as a constant return CGSizeMake(width, height) } func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath)-> CGSize {let width =(screenSize.width-30)/ 2 let height = width * AspectRatio // AspectRatio-someValue或将height作为常量返回CGSizeMake(width,height)}

this worked well for me.. 这对我来说很好。

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

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