Weird Gap in iOS UITableView Cells in iOS 15 - ios

Gap in UITableView Cells
I have added a UITableViewController through code but in iOS and it works okay in iOS 14 but in iOS 15 it shows a weird gap between cells.
I have tried everything that is adding HeaderView, FooterView, two new properties that is introduced in iOS 15
if #available(iOS 15.0, *) {
tableView.fillerRowHeight = 0.0
tableView.sectionHeaderTopPadding = 0.0
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.0
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.0
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
But unfortunately nothing works and the weird gap appears in iOS 15 UITableView.

Related

Grouped UITableView have small extra space on bottom on iOS 15+

Grouped UITableview has an extra small space on the bottom on iOS 15+
this functions doesn't help
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
.leastNonzeroMagnitude
}
tableView.sectionHeaderTopPadding = .leastNonzeroMagnitude
You can try
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
.leastNormalMagnitude
}

Extra Space Between UITableViewSections in iOS 15

UITableView with multiple sections without section footers is shown with an extra space between sections. Xcode's view debugger shows that it's not a view, but just an empty space.
In my case the behavior is unwanted.
Playing with adding a 1.0/0.0 height footer doesn't help. Neither does changing the table view's style.
Here's a sample code:
import UIKit
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .yellow
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.backgroundColor = .green
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = .blue
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 30.0
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.backgroundColor = .red
return footer
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
}
Here's output in iOS 14 and iOS 15:
In iOS 15 the property sectionHeaderTopPadding was added. It affects that exact space. The property's default value is automaticDimension. Setting it to 0.0 fixes the problem.
Since the property is only available in iOS 15, you may want to wrap it with an availability block:
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
}
Here's the original code snippet from the question including necessary changes:
import UIKit
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .yellow
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
}
}
// The rest is without changes.
}
Here's the output in iOS 15 after the change:

UITableView contentSize too big by 10pt (iOS 10 only!)

I am having a strange issue. I have a UITableView set like so:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 210.0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if drawerState == .closed {
return 0
}
return 1
}
Initially the drawerState is .closed so the contentSize is 210. I then hit a button that changes the state and prints the new content size like so:
drawerState = .assessmentOpen
print("old content size is \(tableView.contentSize.height)")
tableView.insertRows(at: [IndexPath(item: 0, section: 0)], with: .none)
print("new content size is \(self.tableView.contentSize.height)")
The result I get is:
old content size is 210.0
new content size is 420.0
The new content size is 10pt more than it should be (new content size should be 410). Where does this excess height come from? Oddly enough if I print the value again after a delay it is correct. I have tried all combinations of layoutIfNeeded and using DispatchQueue.main.async the value is always 10pt more than it should be. What am I doing wrong? NOTE: It only behaves like this on iOS10, iOS11 there are no issues.
If you have a .grouped-styled UITableView then the footer of each section has a default height which is not zero. So to fix that add this:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
If you return just 0. It falls back to the default value. Technically the CGFloat.leastNormalMagnitude is pretty much like a 0. But if you compare it a 0 is smaller than CGFloat.leastNormalMagnitude.
Did you try setting height of footer to zero and returning nil for footer view?
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}

How do I add spacing between two cells in UITableView? There's nothing like separator height or something

I need a layout something like in attached image. I have tried adding a subView at the bottom of UICell but it actually distorts the other items UI. Can someone please help? I am working in Xamarin.iOS
One option is to design the cell as the xib.
One option is to use sections and section footers:
override func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// your spacing
return 20
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.backgroundColor = .clear
footer.isOpaque = false
return footer
}

iOS Swift: Adding bottom insets between section in Table View

I would like to add some space between the last cell of a section and the header of the next section if the table is a plain type
How can I do that?
You can try
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
You can use a Footer View
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
To avoid sticky footer, use allowsFooterViewsToFloat() function
public extension UITableView {
func allowsHeaderViewsToFloat() -> Bool {
return true
}
func allowsFooterViewsToFloat() -> Bool {
return false
}
}
You can do it with just add empty cell at the end of section.

Resources