Display Section Header UICollectionReusableView

referencesizeforheaderinsection swift 4
uicollectionview supplementary view
uicollectionview section header programmatically swift 4
uicollectionview section header swift 4
elementkindsectionheader
viewforsupplementaryelementofkind
uicollectionview header
viewforsupplementaryelementofkind not called

I was working on iOS application and I have several problem about using UICollectionView cell.

This time, I want to ask about how to display the section header of UICollectionView (UICollectionReusableView)

I already implement the function like below :

public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:

            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
            var labelHeader = headerView.viewWithTag(2) as! UILabel

            if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;
            return headerView

        default:
            assert(false, "Unexpected element kind")
        }
    }

but, it always give a blank result. please look at the screen shot below

You need to return size of header .

 func collectionView(_ collectionView: UICollectionView,
                     layout collectionViewLayout: UICollectionViewLayout,
                     referenceSizeForHeaderInSection section: Int) -> CGSize{
       return CGSize(width: CGFloat(collectionView.frame.size.width, height: CGFloat(135)) // you can change here 
    }

Delegate method

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            var reusableview: UICollectionReusableView? = nil
            if kind == UICollectionElementKindSectionHeader {
                reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHea is your identifier
            var labelHeader = reusableview.viewWithTag(2) as! UILabel

         if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;

            }
            return reusableview!
        }

UICollectionView Tutorial: Reusable Views, Selection and Reordering, translatesAutoresizingMaskIntoConstraints = false return view }() override class MyHeaderFooterClass: UICollectionReusableView { let titleLabel = UILabel() let layout = UICollectionViewCompositionalLayout { (section: Int, environment:� The header view is designed to display a section title, while the footer view only shows a static banner image. In Storyboard, drag an image view from Object Library to the header view and add a label on top of it.

I have created demo for you. Download and re-use into your code. Cheers!

Download Link : https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0

Headers with compositionalLayout, Asks the delegate for the size of the header view in the specified section. Availability collectionView. The collection view object displaying the flow layout . What do I need to do to display a header view for only certain sections? Go ahead and return a header for each section and then set the size of the section header to have a size of zero in this UICollectionViewDelegate function.

You need to add UILabel on headerView

      public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
        var labelHeader = headerView.viewWithTag(2) as! UILabel

        if indexPath.section == 0 {
            labelHeader.text = "Specialist Clinic"

        }
        else {
            labelHeader.text = "Medical Support"
        }

        headerView.backgroundColor = UIColor.blue;
        headerView.addSubview(labelHeader) //Add UILabel on HeaderView
        return headerView

    default:
        assert(false, "Unexpected element kind")
    }
}

collectionView:layout:referenceSizeForHeaderInSection:, This is the stretchable header series 3rd part. go to the storyboard and drag and drop a `UICollectionReusableView` to the collectionView. This section defines those "internal" display values, which only have meaning within that particular layout mode. Valid <display-internal> values: table-row-group These elements behave like <tbody> HTML elements. table-header-group These elements behave like <thead> HTML elements. table-footer-group These elements behave like <tfoot> HTML elements.

Stretchable Header view in UICollectionView— Swift 5, iOS, The header view is designed to display a section title, while the footer footer views are associated with the UICollectionReusableView class. I love to use storyboard, so this is a tutorial to provide the steps to adding a header view (footer is similar) to a UICollectionView. 1. Add Header to Storyboard. Select Collection View > Attributes Inspector > Enabled Section Header. Once that is enabled, a section view will appear, and you can drag your views to it.

Use Supplementary View to Add Header and Footer in , In the library there is no any logic/implementation about defining a header/ section view UICollectionReusableView. In the library; Bond /� The action sheet controllers shown above were entirely created using XLActionController and are included in the Examples.To run the Example project: clone XLActionController repository, open XLActionController workspace and run the Example project.

UICollectionView: UICollectionReusableView definition for Sections , For example, a Supplementary View could be used to present a header for a headerView.Text = "Supplementary View Section " + indexPath.Section.ToString � The ActionController class is a generic type that works with any cell, header, section header type and its associated data types. Create your custom action sheet controller XLActionController provides extension points to specify a whole new look and feel to our custom sheet controller and to tweak present and dismiss animations.

Comments
  • Have you tried implementing the reusable view height delegate method?