Swift - how to make custom header for UITableView?

custom section header uitableview swift 4
uitableview header storyboard
uitableview section header scroll with table swift
create custom header for tableview swift
add button to section header uitableview swift
uitableview sticky header swift 4
uitableview section header dynamic height
swift uitableview header text

I need to add custom header to my table

I try this

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18))
    let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
    label.text = "TEST TEXT"
    label.textColor = UIColor.whiteColor()

    self.view.addSubview(view)

    return view
}

but this doesn't work, I see nothing on table

What am I doing wrong ? Or maybe there is another ways ?

Did you set the section header height in the viewDidLoad?

self.tableView.sectionHeaderHeight = 70

Plus you should replace

self.view.addSubview(view)

by

view.addSubview(label)

Finally you have to check your frames

let view = UIView(frame: CGRect.zeroRect)

and eventually the desired text color as it seems to be currently white on white.

Custom Section Header for UITableView - Onur Tuna, Recently two designs catch my attention as an iOS developer: Horizontal lists and custom section headers. Developers and designers make great effort on them  Table View Controller with custom Table View Cells Tutorial in Swift 4, Xcode - iOS - Duration: 24:23. Karthik Thota 4,255 views

The best working Solution of adding Custom header view in UITableView for section in swift 4 is --

1 first Use method ViewForHeaderInSection as below -
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))

        let label = UILabel()
        label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
        label.text = "Notification Times"
        label.font = UIFont().futuraPTMediumFont(16) // my custom font
        label.textColor = UIColor.charcolBlackColour() // my custom colour

        headerView.addSubview(label)

        return headerView
    }
2 Also Don't forget to set Height of the header using heightForHeaderInSection UITableView method -
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

and you're all set 🎉 🎉 🎉

Adding Headers and Footers to Table Sections, To create custom header or footer views: Use UITableViewHeaderFooterView objects to define the appearance of your headers and footers. Register your  How do you make custom headers translucent or semi-transparent just like how you can do with a UINavigationBar? A scroll view is never below the custom UIView header, so making it transparent won't

If you are using custom cell as header, add the following.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = UIView()
        let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
        headerView.addSubview(headerCell)
        return headerView
    }

If you want to have simple view, add the following.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:UIView =  UIView()
    return headerView
}

Custom header view from xib in UITableView, Creating the UITableViewHeaderFooterView class and xib. 1- First of all create a new Swift file and make it subclass of  Oh shoot.. I should have been more clear. I meant adding custom header view for each section in UITableView. Your way seems like to create only one view in tableView. Correct me if I'm wrong. – Kahsn Jan 26 '16 at 8:09

If you are willing to use custom table header as table header, try the followings....

Updated for swift 3.0

Step 1

Create UITableViewHeaderFooterView for custom header..

import UIKit

class MapTableHeaderView: UITableViewHeaderFooterView {

    @IBOutlet weak var testView: UIView!

}

Step 2

Add custom header to UITableView

    override func viewDidLoad() {
            super.viewDidLoad()

            tableView.delegate = self
            tableView.dataSource = self

            //register the header view

            let nibName = UINib(nibName: "CustomHeaderView", bundle: nil)
            self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView")


    }

    extension BranchViewController : UITableViewDelegate{

    }

    extension BranchViewController : UITableViewDataSource{

        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 200
        }

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView" ) as! MapTableHeaderView

            return headerView
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: 

    Int) -> Int {
            // retuen no of rows in sections
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
            // retuen your custom cells    
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        }

        func numberOfSections(in tableView: UITableView) -> Int {
            // retuen no of sections
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            // retuen height of row
        }


    }

UITableViewHeaderFooterView, But did you know that you can do the same for section headers and footers? For example, to achieve 60FPS on a table view with custom cells on an Swift file that declares your UITableViewHeaderFooterView subclass. You could design your own header, like you would design a custom cell in a table view. Or you could just add an image like so: func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { var myCustomView: UIImageView var myImage: UIImage = UIImage(named: "myImageResource") myCustomView.image = myImage let header: UITableViewHeaderFooterView = view as

This worked for me - Swift 3

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
        return headerCell
    }

Custom Header View! (Swift 3 in Xcode), Today we learn about how to make a custom header view for your tableview sections. Enjoy Duration: 15:35 Posted: Dec 6, 2016 Swift 4.X. This is tested and working code. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableViewAutomaticDimension } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "Total Count (41)" } func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int

Customizing Header and Footer of Table View in iOS8 with Swift , Name it TableViewController and make it a subclass of UITableViewController. View fullsize. TableViewController-class.png. You can also simply create ONLY a UIView in Interface builder and drag & drop the ImageView and UILabel (to make it look like your desired header) and then use that. Once your UIView looks like the way you want it too, you can programmatically initialize it from the XIB and add to your UITableView.

How to add a section header to a table view, If you want to return a custom header view with something more than just override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? SPONSORED Build better iOS apps, faster. Today we learn about how to make a custom header view for your tableview sections. Enjoy! :] Leave a comment of what you want the next video to be.

Guide to Customizing UITableView Section Header/Footer, And we will be using awesome swift code :) Step 1: Subclass UITableViewHeaderFooterView. Add your custom class. We call it  690 SwiftUI List is a container that has rows in a single column and you can arrange custom views inside of it. List is the SwiftUI version of UITableview and it’s the easiest and fastest way of building list-style views. Table of Contents Create A SwiftUI Project Rename Root View Structure Model Object & […]

Comments
  • Adding view to self.view is not necessary. It will be removed from there and added to the table view.
  • @Cyrille I'm not sure why you think the view would be removed after it's been programmatically added to the subviews array, but it seems to me it would simply be added to the view hierarchy in two different places, which would lead to undefined behavior.
  • Nope. A view can only have one superview at a time. If you add a view anywhere, it will first be removed from where it was previously.
  • Thank you very much! Especially for remark about ViewDidLoad. Now it works great
  • Please let me know if we can improve this 👍👍
  • You dont have to add additional view there you can create UILabel directly as header. let headerView = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50)), headerView.text = "Notification Times"
  • Thanks for writing back @Hope, yes we can do same as you suggested, I just make it because I need to follow my UI as per Zeplin (the reason I wrote "my custom Colour & font") :)
  • view Hide with Scrolling issue
  • Hi @KrunalNagvadia, what issue you're facing?
  • Very Nice bro. I learnt to manage code using extension as well. Thanks for the full code format! :)
  • Brilliant. I've added a header cell manually and had to offset the cells in the appropriate delegate methods. This'll save some work
  • You need to add the label, yes ; but adding view to self.view is not necessary. It will be removed from there and added to the table view.