Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式将UIImage添加到集合视图单元格中_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 以编程方式将UIImage添加到集合视图单元格中

Ios 以编程方式将UIImage添加到集合视图单元格中,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,这是集合视图单元格的自定义类: import UIKit class NavBarCell: UICollectionViewCell { var avatarImageView: UIImageView = { var avatarView = UIImageView() avatarView.contentMode = .scaleAspectFit return avatarView }() override

这是集合视图单元格的自定义类:

import UIKit

class NavBarCell: UICollectionViewCell {

    var avatarImageView: UIImageView = {
        var avatarView = UIImageView()
        avatarView.contentMode = .scaleAspectFit

        return avatarView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        avatarImageView = UIImageView()
        contentView.addSubview(avatarImageView)
    }

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

}
然后在我的控制器的
viewDidLoad

    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
    navBarCollectionView.setCollectionViewLayout(layout, animated: true)
    navBarCollectionView.backgroundColor = UIColor.clear
    navBarCollectionView.register(NavBarCell.self, forCellWithReuseIdentifier: "cell")
    navBarCollectionView.delegate = self
    navBarCollectionView.dataSource = self
    layout.scrollDirection = .horizontal
    self.navigationController?.navigationBar.addSubview(navBarCollectionView)
    navBarCollectionView.reloadData()
cellForItem
中,我有:

        let navBarCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)) as! NavBarCell

        var image : UIImage = UIImage(named: "TestImage")!
        navBarCell.avatarImageView.image = image
        navBarCell.avatarImageView.layer.borderWidth = 1
        navBarCell.avatarImageView.layer.borderColor = UIColor.getRandomColor().cgColor
        navBarCell.avatarImageView.layer.cornerRadius = 20

        return navBarCell
但是图像视图没有显示出来。如果我添加
navBarCell.backgroundColor=UIColor.red
,则会显示单元格,但不会显示图像


我遗漏了什么,或者没有正确实现什么?

最佳做法是创建一个原型单元,并在interface builder中添加图像视图。在这个特定场景中,我认为错误在于没有将图像视图的帧设置为缩放到单元格帧


尝试打印图像视图的框架,它可能会显示0,0,0,0。

最佳做法是创建一个原型单元,并在界面生成器中添加图像视图。在这个特定场景中,我认为错误在于没有将图像视图的帧设置为缩放到单元格帧


尝试打印出图像视图的框架,它可能会显示0,0,0,0。

UICollectionViewCell
已经是一个视图,您可以直接向其添加
avatarImageView

addSubview(avatarImageView)
您还可以设置约束,例如:

avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
avatarImageView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
avatarImageView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
avatarImageView.heightAnchor.constraint(equalToConstant: 300).isActive = true

UICollectionViewCell
已经是一个视图,您可以直接向其中添加
avatarImageView

addSubview(avatarImageView)
您还可以设置约束,例如:

avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
avatarImageView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
avatarImageView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
avatarImageView.heightAnchor.constraint(equalToConstant: 300).isActive = true

您需要在初始化时设置avatarImageView的帧。如果希望图像视图缩放到单元格的帧,请在
cellForItem
处重置帧或使用约束。需要在初始化时设置
avatarImageView
的帧。如果希望图像视图缩放到单元格的边框,请在
cellForItem
处重置边框或使用约束。非常好,谢谢。我不知道您可以直接添加子视图。一切都好了,再次感谢!这不太正确。您应该将图像视图添加到单元格的
contentView
,而不是单元格本身,就像问题代码中所做的那样。太好了,谢谢。我不知道您可以直接添加子视图。一切都好了,再次感谢!这不太正确。您应该将图像视图添加到单元格的
contentView
,而不是单元格本身,就像在问题代码中所做的那样。