Stan92 Stan92 - 4 months ago 81
Swift Question

Swift 2.2 UITableViewHeaderFooterView transparent

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

Answer

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