ios - Swift UICollectionView 单元格宽度

标签 ios swift uicollectionview constraints

我试图在不使用界面生成器的情况下构建 Collection View ,但我无法调整单元格的宽度。单元格似乎保持其最小正方形大小,我希望单元格能够拉伸(stretch) View 的宽度。

我对约束有什么误解?

具体来说:

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

似乎没有调整单元格的宽度或高度,完整示例:

import UIKit

class UserCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "foo"
        collectionView?.backgroundColor = UIColor.red
        collectionView?.alwaysBounceVertical = true
        collectionView?.register(UserCell.self, forCellWithReuseIdentifier: "cellId")
    }

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

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

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

class UserCell:UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let nameLabel: UILabel = {
        let label = UILabel()
        label.text = "foo"
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.boldSystemFont(ofSize: 14)
        return label
    }()

    func setupView() {
        addSubview(nameLabel)
        let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])
        addConstraints(hConstraints)
    }
}

最佳答案

What am I getting wrong with the constraints?

没有。 UICollectionViewFlowLayout 下的 UICollectionViewCell 的宽度与其内部约束无关。 (应该有一个允许这样做的功能,但它对我来说从来没有用过。)

您可以通过设置流布局的 itemSize 或通过在集合中实现 collectionView(_:layout:sizeForItemAt:) 来显式设置宽度 View 的委托(delegate)(通常是 UICollectionViewController,并且在任何情况下都必须明确采用 UICollectionViewDelegateFlowLayout,正如 Rob 的回答中告诉您的那样)。

所以只要改变这个:

class UserCollectionViewController: UICollectionViewController {

为此:

class UserCollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {

大功告成。

关于ios - Swift UICollectionView 单元格宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352011/

相关文章:

java - 为什么 Swift 不隐式地为每个类提供 hashable?

ios - 使用两种不同类型的单元格时如何确定 Collection View 的大小

iOS - 对 View 中的多个 Tableview 或 Collectionview 使用/传递手势识别器(Swift)

ios - 我是否需要 ios sdk 7.0 才能使用 swift 在 xcode 6 beta 中开发 ios 7 应用程序

ios - 无法绘制旋转图像

ios - dismissViewControllerAnimated 不会关闭

ios - 如何在 UICollectionView 的顶部添加一个 View 以支持 objective-c 中的搜索栏或过滤器之类的东西?

swift - 如何使用位置将 Swift 子字符串替换为不等于子字符串?

ios - 在获取异步数据时,在获取所需结果之前提前调用通知 block 。想要在获取所有数据后重新加载 collectionView

iOS:使用 Three.js 开发的 HTML5