How to dye UITableView bottom empty space? - ios

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
}

Related

Keyboard pushes FooterView up when its opening < TextView > Swift

I have a problem that I couldn't find any appropriate solution.
I have a tableView with custom cells which holds a textView inside.
also I have a blank grey footer for the section.
whenever I click on the textView and the keyboard pops up the footer in that section goes up and hides behind the textView field.
I don't know how to make that footer sticky so it won't move when the keyboard pops.
if anyone have a solution it will be great!
thank you!
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 0 {
let footerView = UIView()
footerView.backgroundColor = .red
return footerView
}else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 8
}
Set your tableView as grouped give height to the tableview footer and tableView Header in the tableview header,footer height functions.
If you are not using tableview header the set the height of the header to .leastNormalMagnitude .
This is the way to make the tableView Footer Static.

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!

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
}

Prevent footer overlapping tableViewCell in UITableView - Swift

I have this table view which works how i want it to however i have a problem where the footer overlap the cells in the table view as seen in
How can i prevent this? This is my code for the footer
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
// self.myTableView.tableFooterView = footerView;
let sectionString = Array(foodArray.keys)[section]
for value in caloriesArray[sectionString]! {
calories += value
}
totalCalories += calories
print(calories)
print(totalCalories)
let label = UILabel(frame: CGRectMake(footerView.frame.origin.x - 15, footerView.frame.origin.y, footerView.frame.size.width, 20))
label.textAlignment = NSTextAlignment.Right
label.text = "Total Calories: \(calories) "
footerView.addSubview(label)
calories = 0
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
#IBAction func addFoodTapped(sender: AnyObject) {
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let sectionString = Array(foodArray.keys)[indexPath.section]
foodArray[sectionString]?.removeAtIndex(indexPath.row)
caloriesArray[sectionString]?.removeAtIndex(indexPath.row)
print(foodArray)
viewDidAppear(true)
}
You can do that, Just make Style Grouped as shown below:
Just add some padding to the bottom of the content so that it doesn't overlap with the footer:
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, FOOTER_HEIGHT, 0)
I know this is an old thread, so this is more so for others that encountered this (like myself).
The floating section footer is default behavior from my understanding. There are a couple of options that I can think of:
Provide your footer view with a background color (footerView.backgroundColor = UIColor.white) thus cleaning up the overlap.
or
Replace the section footer with a custom "Total Calories" cell that you add to the table after the last cell in that section, effectively acting like a footer cell.
Sorry for the delay. If you have modified the standard contentInsetAdjustmentBehavior of the tableView, you must adjust the tableView contentInset property to take into account the total height of the views at the bottom of the UITableView, like the tab bar. If contentInsetAdjustmentBehavior is set to "automatic" (or you didn't change the default value), then set the clipsToBounds property of your footer view to true so that its child views cannot be painted outside the footer view layer's frame. That should solve your issue.
Try to override this method
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 44.0
}
Just set your label's background color to UIColor.white. and you are done !
label.backgroundColor = UIColor.white
Of course it overlaps. This is how footers and header work in UITableViews. You can set the footerView.backgroundColor to UIColor.gray, for example, to make it look better.

Resources