2

I'm trying to set a transparent background to a TableViewHeader but without success. First of all, would like to know if it's possible?

This is what I've done

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
   let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
   header.contentView.backgroundColor = UIColor.clearColor()
}

My final objective is to add under my uitableview an UIView that'll host a GMSMapView and have a transparent HeaderView with a size of 100px.

Any help? Thanks

Stan92
  • 453
  • 1
  • 8
  • 21

3 Answers3

4

You need to do following steps

1) in storyboard Add headerfooterview to tableview

2)In view did load write below code

tableView.registerClass(HeaderView.self, forHeaderFooterViewReuseIdentifier: "HeaderView")
headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("headerView") as? HeaderView

3) add tableview delegate methods

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

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    headerView.backgroundColor = UIColor.clearColor()
    return headerView;
}

Hope this will help you

Kuntal Gajjar
  • 792
  • 6
  • 12
2

Steps to follow:

1) Drag and drop a UIView from object library to the top of the tableview to make a tableview header.

2)make a IBOutlet of the view in the Tableview controller.

3)lastly call this UITableview delegate Method

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

        yourHeaderView.backgroundColor = UIColor.clearColor()
        return yourHeaderView

    }

// this is called when you need to make a transparent footer view

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        footerView.backgroundColor = UIColor.clearColor()
        return footerView
    }
Anil Kumar
  • 945
  • 7
  • 16
0

//for transparent your table view section header:

//In Swift 5

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

view.tintColor = UIColor.clear

}

Shamshad
  • 129
  • 1
  • 6