将图像添加到UICollection标头Swift

时间:2019-03-29 21:31:58

标签: swift

我正在寻找在集合视图的标题内实现图像的方法。这是我到目前为止的代码,但是在测试时没有标题出现。我想念什么吗?

    func collectionView(_ collectionView: UICollectionView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    let image = UIImageView()
    image.frame = CGRect(x: collectionView.frame.width - 10 , y: 0, width: 20, height: 20)
    image.image = UIImage.init(named: "trophyCase")
    view.addSubview(image)
    return view
}

2 个答案:

答案 0 :(得分:1)

UICollectionViewDelegate不提供viewForHeaderInSection

这样的方法

相反,您可以使用viewForSupplementaryElementOfKind的{​​{1}}方法

UICollectionViewDataSource

您还需要注册func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { guard kind == UICollectionView.elementKindSectionHeader else { fatalError("Unrecognized element of kind: \(kind)") } let view: ReusableHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kind, for: indexPath) as! ReusableHeaderView view.imageView.image = UIImage.init(named: "trophyCase") view.imageView.frame = CGRect(x: collectionView.frame.width - 10 , y: 0, width: 20, height: 20) return view }

elementKindSectionHeader

以下将是您的collectionView.register(ReusableHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: UICollectionView.elementKindSectionHeader)

ReusableHeaderView

答案 1 :(得分:1)

创建一个继承UICollectionReusableView的视图:

class HeaderView: UICollectionReusableView {

    let imageView: UIImageView = {
        let iv = UIImageView(image: /*put your image here*/)
        iv.clipsToBounds = true
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(imageView)
        imageView.fillSuperview() // Check extension below
    }


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

然后在您的ViewController中,首先为您的视图创建一个redirectIdentifier:

fileprivate let headerId = "headerId"

之后,将您的自定义视图注册到collectionView中(可在viewDidLoad中进行操作):

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
}

在vc中将您的自定义视图声明为可选:

var headerView: HeaderView?

然后覆盖viewForSupplementaryElementOfKind的{​​{1}}方法以初始化collectionView

headerView

然后实施另一种override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as? HeaderView return headerView! } 方法来为您的collectionView设置大小:

headerView

用于自定义视图初始化的func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return .init(width: view.frame.width, height: 340) // edit width and height as you please. } 的扩展名:

fillSuperView

现在它应该可以作为extension UIView { func fillSuperview(withPadding padding: UIEdgeInsets = .zero) { translatesAutoresizingMaskIntoConstraints = false if let superview = superview { topAnchor.constraint(equalTo: superview.topAnchor, constant: padding.top).isActive = true leftAnchor.constraint(equalTo: superview.leftAnchor, constant: padding.left).isActive = true rightAnchor.constraint(equalTo: superview.rightAnchor, constant: -padding.right).isActive = true bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -padding.bottom).isActive = true } } } 的标题视图。