Reputation: 1635
I need to reduce the space between two sections ofUITableView
. I looked at this question but the solution doesn't allow for my custom header view because it combines the space of the footer and header.
Here is a picture of the UITableView
. The black color is the UITableView
background color.
Upvotes: 133
Views: 124934
Reputation: 345
If I'm not mistaken you want to remove the gap between your sections and the last cell from the last section. If this is the case you should check a couple of things but it is mainly because of the footer. And if it is because of the footer you should try to both return nil for viewForFotterInSection and return 0 for heightForFooterInSection.
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
And if your case is not because of the footer you can check out these:
insetForSectionAt function -> return zero for this function
func tableView(_ tableView: UITableView, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.zero
}
Check for the table view style and play with it. Try to set it as "grouped".
UITableView(frame: .zero, style: .grouped)
Check your cell view layout
Try to set the style to grouped and set the footer view to nil and its height to zero
Upvotes: 1
Reputation: 51
If you need spacing only for the first cell at the top of your UITableView and your header sections after scrolling must have no space, instead of using contentInsets.top
do this:
Add fake UIView
with your spacing by adjusting it's height
and place it into UITableView.tableHeaderView
Here's how it should look like (using SnapKit):
let spaceView = UIView()
spaceView.snp.makeConstraints {
$0.height.equalTo(10)
}
tableView?.tableHeaderView = spaceView
tableHeaderView
is not fixed, so in this case, spacing at the top of tableView
will be 10
, but after scrolling tableView
spacing from top will be gone, spacing only stays at the top.
Upvotes: 1
Reputation: 69
This will work iOS 15 and greater
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
It will reduce automatic space between header and cell, if you have multiple header sections.
Upvotes: 2
Reputation: 14845
Did you try override this function:
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
Upvotes: 176
Reputation: 2091
TableView Delegate methods doesn't effect with float value is 0.0f. Try giving a value greater than that.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.00001f;
}
- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
Upvotes: 3
Reputation: 61
swift 5 iOS 15
self.tableView.estimatedSectionFooterHeight = 16.0 // spacing between Sections
Upvotes: 0
Reputation: 618
I just simply had to reduce the top padding for the tableview section header:
tableView.sectionHeaderTopPadding = 0
Upvotes: 7
Reputation: 11
Work for me
tableView.sectionFooterHeight = 10
// ...
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
Upvotes: 1
Reputation: 85
In Xcode 13.2, you can adjust the height of the header and footer of sections in the storyboard - see screenshot below:
Upvotes: 3
Reputation: 5712
You need to use the method heightForHeaderInSection
for defining the space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN
which is 0.000001f. Giving you an example, how you can use different section with different header heights:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
Upvotes: 6
Reputation: 1232
For Swift 5+:
There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.
My solution to having a separation between 2 sections is the following:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 24
} else if section == 1 {
return 32
} else {
return 40
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
CGFloat.leastNormalMagnitude
}
As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.
In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.
Upvotes: 10
Reputation: 9983
On iOS 15 you may want to reduce the sectionHeaderTopPadding
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
Upvotes: 254
Reputation: 2814
Rather than implementing the UITableViewDelegate
methods and defining the sectionFooterHeight via CGFloat.leastNormalMagnitude
, one can alternatively just
tableView.sectionFooterHeight = 0
and the spacing between sections while no footer is present will go away.
The mechanism is that by default this value is set to UITableView.automaticDimension
.
As long as
UITableView.automaticDimension
titleForFooterInSection
/viewForFooterInSection
.grouped
then UITableView will deliberately insert a spacing between sections with no view.
You change sectionFooterHeight
to 0, the magic goes away.
Upvotes: 3
Reputation: 126
Select the tableView
in your storyboard/objectCode and ensure that the style is set to Plain
, instead of Grouped
. You can find this setting in the attributes "Inspector" tab.
let myTableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
tableView.separatorStyle = .none
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
Upvotes: 2
Reputation: 1013
For Swift 4+ you need to implement these two methods
extension MyViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
}
Upvotes: 33
Reputation: 769
Along with the answer posted by Icaro I would like to add that you also need to implement the tableView:viewForFooterInSection:
method returning nil
for the section you want to remove the empty space below
It will then become:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
Upvotes: 7
Reputation: 2519
This also may help :
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
Upvotes: 2
Reputation: 3875
For Swift 3
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
Upvotes: 13
Reputation: 571
You can do it by implement the delegate heightForHeaderInSection
& heightForFooterInSection
.
The return vaule should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it need a very small value, try CGFLOAT_MIN
.
for my example:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
return 46;
}
return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
Upvotes: 0
Reputation: 2020
I think you can solve this by adjusting the footer view height to its min: in Storyboard or XIB.
I don't know what you have written in your code for footer height. Sorry if I am wrong.
Possible duplicate of Hide footer view in UITableView
Upvotes: 44