1

I need to use my UIView custom class in UICollectionViewHeader

Here is my UIView class code

class ViewPlayerHeader: UIView {
    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var imgViewCoverPhoto: UIImageView!
    @IBOutlet weak var imgViewUserPic: UIImageView!
    @IBOutlet weak var lblPlayerName: VVLabel!
    @IBOutlet weak var lblCountryname: VVLabel!
    @IBOutlet weak var lblPlayedFor: VVLabel!

    var arrSportsIcon:NSMutableArray!

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override init(frame: CGRect) { // for using CustomView in code
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
        super.init(coder: aDecoder)
        commonInit()
    }

    override func draw(_ rect: CGRect) {
        // Drawing code
    }

    private func commonInit () {
        Bundle.main.loadNibNamed("ViewPlayerHeader", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        imgViewUserPic.layer.borderColor = UIColor.white.cgColor
        imgViewUserPic.layer.borderWidth = 2.0
    }
}

Updated -> add colleciton view header code

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let headerView = ViewPlayerHeader.init(frame: CGRect(x: 0, y: 0, width: self.collectionViewInfo.frame.size.width, height: 50))
            return headerView
        default:
            assert(false, "Unexpected element kind")
        }

}
Vivek
  • 4,916
  • 35
  • 40
  • check this: https://stackoverflow.com/a/42200691/7851805 – Nick Oct 18 '19 at 07:02
  • 1
    Yes, I know with `UICollectionReusableView`, But I don't want to create `UICollectionReusableView` I already created `UIView` class, and this `UIView` class is using in multiple times in my application. – Vivek Oct 18 '19 at 07:26

0 Answers0