UITableView Section header color misbehaving - ios

I changed the color of my custom UITableViewCell section header and while I'm scrolling inside my tableView it changes its alpha, I guess. Some are kind of transparent and the others have a solid color.
What is wrong?
// Customize HeaderView of Sections
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This changes the header background
view.tintColor = UIColor(hue: 0.13, saturation: 0.13, brightness: 0.13, alpha: 1)
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour
var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel.textColor = UIColor.whiteColor()
}

I would strongly suggest to configure your header view in this method:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
I would use the willDisplayHeader... for other purposes than primarily configuring the appearance.

Related

How to change UITableviewController Static cell section background color change using Swift?

My Scenario, I am trying to change the UITableviewController static cell header background color by using below code. I checked cell color, content view color clear and fixed tableview background color black, but section color not changing. How to fix it?
self.tableView.backgroundColor = .black
self.mycell.backgroundColor = .clear
In your UITableViewDelegate override the following to modify header view:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = .green
}

How to resize and change color of headlines in tableview xcode

I am using a static Tableview on my storyboard
that has headlines to the sections
The headlines text color and size is a static thing and I cannot change it
it results in very narrow headlines and black text.
how can I space out the headlines (make the height a bit bigger) and change the color of the text?
how can i space out the headlines (make the height a bit bigger) and change the color of the text ?
You'll need to have a custom view that has a label, and return it to the delegate method of the UITableView's viewForHeaderInSection.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Ref:
https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-section-header-to-a-table-view
https://developer.apple.com/documentation/uikit/uitableview/1614965-headerview
EDIT:
Here's how to achieve this. Basically you'll need a custom view in the delegate method I mentioned above. If you've successfully made a custom UITableViewCell in your cellForRow before, then this one should be a piece of cake for you.
You declare a container view, and then add your subviews in that container, in your case, a UILabel. I always use constraints, like so:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Let's make the even numbers sections a red background.
// Blue background for odd numbers
let container = UIView()
container.backgroundColor = section % 2 == 0 ? .red : .blue
let titleForHeaderLabel = UILabel()
titleForHeaderLabel.backgroundColor = .white
titleForHeaderLabel.text = "HEADER SECTION: \(section)"
container.addSubview(titleForHeaderLabel)
titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
return container
}
Then you provide a height for your section in the func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat delegate method, like so:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80.0
}
Output:
Easy, right? :) I hope this helps!

Section header title lost when customize the section header view

In my code I use below code to set the section header title of the tableview. And it works well.
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
//return self.adHeaders[section]
return self.headers[section]
}
I want to customize the background color of the header, so I insert below code before the above code.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor(red: 232/255.0, green: 237/255.0, blue: 242/255.0, alpha: 1.0)
return headerView
}
As a result, the background color changes, while the title text lost.
When you implement viewForHeaderInSection, titleForHeaderInSection isn’t called at all. So you need to set the title of your header view in viewForHeaderInSection. You probably need to create a UILabel.
You must also implement heightForHeaderInSection.

How to dye UITableView bottom empty space?

I have a table with white empty area at the bottom of table view. But I want this area to be gray. I have added a gray footer but it is too much small.
What shall I do?
Set tableView's backgroundColor as gray color and tableFooterView with empty UIView
self.tableView.tableFooterView = UIView()
self.tableView.backgroundColor = .gray
Add the below lines,
self.tableView.tableFooterView = UIView()
It will works perfectly.
if its because of tableview footer then use the below delegate methods else if its the background view colour modify you tableview to fit the parent view rect
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}

Controlling UITableView Headers changing opacity in iOS 10

When my table view loads it's headers are translucent, but if I scroll it off screen and bring it back, it is opaque.
Initially I wanted them to be opaque, but I'm not sure now so I'd like a solution for either scenario. Changing isOpaque to false (the default) has the same behaviour.
My delegate method:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = .headerColor
view.isOpaque = true
(view as! UITableViewHeaderFooterView).textLabel?.textColor = .white
}

Resources