简体   繁体   English

UIcollectionview单元格问题Swift

[英]UIcollectionview cell issue Swift

I have a simple collectionview controller like this: 我有一个简单的collectionview控制器,如下所示:

class ViewController1: UICollectionViewController{

    override func viewDidLoad() {

        super.viewDidLoad()

        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self

    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 5
    }

override func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath)

        return cell
    }
}

In the cell there is only an uiimageview and it has this constraints: 在单元格中只有一个uiimageview,它有这样的约束:

在此输入图像描述

My problem is this: 我的问题是:

when I execute this code on iPhone 6 simulator I get this (right way): 当我在iPhone 6模拟器上执行此代码时,我得到了这个(正确的方法): 在此输入图像描述

but when I execute this code on iPhone 5 simulator I get this (wrong way: the imageview starts before the screen on the left and the width and height are wrong respect to the constraints): 但是当我在iPhone 5模拟器上执行此代码时,我得到了这个(错误的方式:imageview在左边的屏幕之前开始,宽度和高度相对于约束是错误的): 在此输入图像描述

I am going crazy for this problem but I dont be able to solve. 我为这个问题疯狂,但我无法解决。 Can you help me? 你能帮助我吗?

You need to extend your ViewController1 to conform to UICollectionViewDelegateFlowLayout protocol. 您需要扩展ViewController1以符合UICollectionViewDelegateFlowLayout协议。 And implement methods to set size for each collection view cell like so: 并实现为每个集合视图单元格设置大小的方法,如下所示:

class ViewController1: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {

    super.viewDidLoad()

    self.collectionView?.delegate = self
    self.collectionView?.dataSource = self
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    print(self.collectionView!.frame)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 5
}

override func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath)

    return cell
}

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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.collectionView!.frame.size.width, height: 120.0)
}
}

It has hard coded values, but you should get the idea. 它有硬编码值,但你应该明白这个想法。 Collection View width should be the max size for each item, so they won't get outside the box. 集合视图宽度应该是每个项目的最大大小,因此它们不会超出框。

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

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