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
}
Related
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
}
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!
We're want to do two changes to our search results.
Change the Collection View Controller to a Table View Controller
Have only the second section header show and have it scroll away inline. In other words, don't be sticky and don't stay at the top.
In regards to changing the Collection View Controller to a Table View Controller. This involves changing the section headers from a UICollectionReusableView subclass to something else. Normally, the change would be to using a UITableViewHeaderFooterView.
In regards to making the second section header scroll on up inline and out of sight and thus not be sticky, there's an issue to my solution.
To make it non-sticky, I changed the tableview to be UITableViewStyleGrouped. This causes a look for the second section that is different than what we have now. We would like it back to how it looks now.
Here's what we have in the App Store now:
Here's what we have when trying the UITableViewStyleGrouped approach and other changes:
Notice the extra gray above the "Other cars..." section header? I figure I can probably solve the rounded corners and insets via a subclass of the UITableViewHeaderFooterView. However, it's not clear how to handle the extra space above the section.
Code:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 800
self.tableView.rowHeight = UITableViewAutomaticDimension
let nib = UINib(nibName: "SearchResultsOtherCarsHeader", bundle: nil)
tableView.register(nib, forHeaderFooterViewReuseIdentifier: "SearchResultsOtherCarsHeader")
:
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section != 1 {
return 0
}
return 30
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section != 1 {
return nil
}
let searchResultsOtherCarsHeaderView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "SearchResultsOtherCarsHeader") as! SearchResultsOtherCars
return searchResultsOtherCarsHeaderView
}
How to get rid of the extra gray space above the section header?
The solution:
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
This solution was inspired by the comment made by #maddy and iPhone UITableView : How to remove the spacing between sections in group style table?
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
}
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.