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
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
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
}
//for transparent your table view section header:
//In Swift 5
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.clear
}
Related
How can I swipe header view same like tableview cell can any one please help me out
override func viewDidLoad() {
super.viewDidLoad()
let headerNib = UINib.init(nibName: "RequestHeaderCell", bundle: nil)
tableVIew.register(headerNib, forHeaderFooterViewReuseIdentifier: "RequestHeaderCell")
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header: RequestHeaderCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "RequestHeaderCell") as! RequestHeaderCell
let userData = requestData[section]
header.userData = userData
return header
}. // how can I make this swipe to right
height for header view:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 90
}
headerView class:
class RequestHeaderCell: UITableViewHeaderFooterView {
}
Simply return cell.contentView instead of cell within your viewForHeaderInSection function may be your problem will be resolved.
I want to customize my section headers for a UITableView. How do I add text color, background color, and section header height?
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.black
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
I'm trying to fix the position of a search bar in a tableViewController, I know how to do it in the navigation bar, but I can't find any documentation on how to do it without a navigation bar.
1.Create cell xib and set the search bar in cell ( ex: your cell name is SearchBarCell )
2.Now set the SearchBarCell as "viewForHeaderInSection"
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchBarCell") as! SearchBarCell
return cell
}
give section height
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
I found similar questions, but none of them helped me.
I need to set the background colour of headers in my table to white, it is gray now(same as background colour of the table).
Here are my tableView functions:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header = view as! UITableViewHeaderFooterView
header.backgroundColor = UIColor.white
header.textLabel?.font = UIFont(name: "Futura", size: 13)!
header.textLabel?.textColor = UIColor.blue
}
func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
tableView.backgroundColor = UIColor.white
return tableView
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return titles[section]
}
I'm setting background colour to white, but nothing changes.
Swift 3 - What did the trick for me was setting the header's contentView's backgroundColor.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
header.contentView.backgroundColor = .blueSteel
return header
}
Hope that helps someone.
Use viewForHeaderInSection method of UItableview and Create UIview
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}
In Swift 3, this worked for me:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.font = UIFont(name: "Futura", size: 13)!
headerView.backgroundView?.backgroundColor = .white
}
Swift 3 below. One notable difference to previous answer, don't forget the override keyword. If you do, it won't override the function and will not work.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.darkGray
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}
I have a UITableViewController that I'm using to override the inputView of a UITextField subclass. Sometimes the tableView didSelectRowAtIndexPath stops getting fired until a user scolls the table and tries to click again. I can't seem to get a fix on what would cause the cells to become unresponsive. Most of the time it works great, but randomly the cells stop responding to touches until the user scrolls the tableview a bit.
I don't have any TapGestureRecognizers in the view to my knowledge, and have played around with overriding HitTest in the UITableViewCell, but can't seem to figure out the issue.
Any ideas on how to better debug this?
Here is the tableView controller code:
protocol MultiSelectPickerDelegate
{
func didSelectOption(optionIndex:Int, group:Int)
}
class MultiSelectPicker: UITableViewController {
var multiSelectGroups = [MultiSelectGroup](){
didSet{
self.tableView.reloadData()
}
}
var delegate:MultiSelectPickerDelegate?
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return multiSelectGroups.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return multiSelectGroups[section].multiSelectOptions.count
}
//MARK: UITableView datasourse function
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
delegate?.didSelectOption(indexPath.row, group: indexPath.section)
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
let label = UILabel(frame: CGRectMake(10, 0, tableView.frame.size.width, 40))
label.font = UIFont.systemFontOfSize(14)
label.text = multiSelectGroups[section].name
label.textColor = UIColor.whiteColor()
view.addSubview(label)
view.backgroundColor = UIColor.MyAppBlue()
return view
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil{
cell = UITableViewCell(style: .Default, reuseIdentifier: "reuseIdentifier")
}
cell?.textLabel?.text = multiSelectGroups[indexPath.section].multiSelectOptions[indexPath.row].name
return cell!
}
}
I was having same problem. On my table view it was not clicking very last row of my table on the first tap. It highlights it, but didSelectRowAtIndexPath isn't being called. All the other rows work fine. But if I turn the tableview bounce on then it seems to solve the problem
Also try removing Show Selection on Touch in Interface Builder as follows:
Hope it helps