Change the sections header background color in UITableView using an array of headers

uitableview section header text color
uitableview hide section header when scrolling
uitableview section header scroll with table swift
uitableviewheaderfooterview background color
uitableview section footer background color
uitableview section header storyboard
swiftui section header background color
uitableview section header font

I have a array of headers that I use

let sectionHeaderTitleArray = ["test1","test2","test3]

and they are showed using

func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
} 

Now all of this works fine but I would like to modify the background color of the headers so that they are more visible (Darker Color)

any idea if I can do this in a simple line or do I need to use a custom cell to create this

thanks

update

  //number of sections and names for the table

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.sectionHeaderTitleArray.count
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sectionHeaderTitleArray[section] as String
    }
    func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
        return self.tableView.backgroundColor = UIColor.lightGrayColor()
    }

Instead of using the

func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String?

data source method, you can use the

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

delegate method and simply customize the UIView returned as you wish.

For example set the text of the UILabel textLabel to your desired value and the backgroundColor to the desired UIColor.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let returnedView = UIView(frame: CGRectMake(x, y, width, height)) //set these values as necessary
    returnedView.backgroundColor = UIColor.lightGrayColor()

    let label = UILabel(frame: CGRectMake(labelX, labelY, labelWidth, labelHeight))
    label.text = self.sectionHeaderTitleArray[section]
    returnedView.addSubview(label)

    return returnedView
}

SWIFT 5

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let returnedView = UIView(frame: CGRect(x: x, y: y, width: width, height: height)) //set these values as necessary
            returnedView.backgroundColor = .white

            let label = UILabel(frame: CGRect(x: x, y: y, width: width, height: height))

            label.text = self.sectionHeaderTitleArray[section]
            returnedView.addSubview(label)

            return returnedView
        }

ios, I have a array of headers that I use let sectionHeaderTitleArray = ["test1","test2","​test3]. and they are showed using func tableView[tableView:  Questions: I have a array of headers that I use let sectionHeaderTitleArray = ["test1","test2","test3] and they are showed using func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return self.sectionHeaderTitleArray[section] as String } Now all of this works fine but I would like to modify the background color of the headers so that they are

If you're using only titleForHeaderInSection :

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as! UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
    (view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.white
}

Manipulating Visible Headers In Section Using Swift, I have a array of headers that I use let sectionHeaderTitleArray = ["test1","test2","​test3]. and they are showed using func tableView[tableView:  and they are showed using. func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return self.sectionHeaderTitleArray[section] as String } Now all of this works fine but I would like to modify the background color of the headers so that they are more visible (Darker Color)

You have to keep both

titleForHeaderInSection

AND

viewForHeaderInSection

Here is some working code in Swift 3

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sectionHeaderTitleArray[section]
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
    returnedView.backgroundColor = .lightGray

    let label = UILabel(frame: CGRect(x: 10, y: 7, width: view.frame.size.width, height: 25))
    label.text = self.sectionHeaderTitleArray[section]
    label.textColor = .black
    returnedView.addSubview(label)

    return returnedView
}

Adding Headers and Footers to Table Sections, i have array of headers use let sectionheadertitlearray = ["test1","test2","test3]. and showed using func tableview[tableview: uitableview,  To change the background color, text label color and font for the Header View of a UITableView Section, simply override willDisplayHeaderView for your table view like so: override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header = view as!

SWIFT 4

Super easy, by setting header's view, contentView background color..

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
   let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
   header.contentView.backgroundColor = AnyColor
   return header
}

Customizing Header and Footer of Table View in iOS8 with Swift , Ever noticed that Apple's Photos app is changing its headers-in-section to blur once the header-in-section has reached its sticky position, the background is… The ability, that NOT all headers-in-section could change their color — some can to maintain a robust array of headers-in-section that its type, inherits only from  Change the class to TableSectionHeader; In the “Table Section Header” view, make sure the background color is default. This is to avoid the warning Setting the background color on UITableViewHeaderFooterView has been deprecated. Add a View. This is a container and MUST be there to contain all the other subviews.

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) {
    view.tintColor = UIColor.lightGray
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.darkGray
    header.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}

Change font of a Table Header Section, Use header and footer views as visual markers for the beginning and end of Always use a UITableViewHeaderFooterView object for your headers and footers​. To change the background behind your content, modify the backgroundView​  I want to customize TableView section header and to leave default background color. I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section. I need to change font size depending on device (iPad or iPhone).

UICollectionView Tutorial: Reusable Views, Selection and Reordering, In this tutorial we are going to fill the table with some sample country names UITableViewCell // 3 // Configure the cell switch (indexPath.section) { case 0: cell. Each array contains 3 countries so the number of rows per section will A View is created with a height of 40 and a black backgroundColor. First question,is there any way to get default section header? If it’s not possible, i need to create a container view which is a UIView but,this time i need to set default background color,shadow color etc. Because, if you look carefully into section’s header, it’s already customized. How can i get these default values for each section

UITableView Class (UIKit), Change the sections header background color in UITableView using an array of headers. I have a array of headers that I use let sectionHeaderTitleArray  if you also want to tweak the color in the section header for each section, then supply a custom header with something like this: (1) in your viewDidLoad() function, include a definition of what you use for a class for the section headers.

To display this section header, you'll use UICollectionReusableView . This class is similar to UICollectionViewCell , except it usually displays headers and footers. Table View Background Color to give it a nice offset from the rest of the collection view. UICollectionView can animate layout changes. To customize the header color, the only option (that I know of) you have is to completely replace the default header with a UILabel, and change the properties of the UILabel to meet your needs. UILabel is a descendant of UIView, so you can pass it in to the HeaderView of a table section.

Comments
  • possible duplicate of UITableView - change section header color
  • well you are returning a UIColor when it expects a UIView..
  • you need to set it with returnedView.backgroundColor = UIColor.lightGrayColor() where returnedView is the UIView you initialize in func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
  • see my updated answer
  • thanks this changes the color correctly but my width and length settings are all setup in interface builder with autolayout.. how can I get this to adapt to the screen size?
  • Thanks but i can use my array of sections title in this
  • So set the textLabel.text property to the desired section title as I suggested :)
  • I need all my sections to have the same lightgreycolor
  • So set the backgroundColor to the desired UIColor (lightGrey in your case)
  • Setting background color directly to header view raises runtime warning Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.