Reputation: 1915
When you create a UITableView
with the UITableViewStyleGrouped
style, it adds quite a lot of space in between the actual tableviewcells and the borders of the table's frame. This space is above, below, to the left, and to the right of the tableviewcells.
You can change the space between tableviewcells themselves by:
[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];
However, even by doing that, there's still this annoying space between the top of the frame and the first tableviewcell. Furthermore, there's a ton of space still in between the left of the frame and the tableviewcell, as well as the right side of the frame and the tableviewcell.
-=-=-=-=-=-=-
Is there a way to manipulate that space (resize it)? My only solution thus far is to make the size of the frame larger than the screen to fake out the program into having that "blank space" outside of the screen, thus removing it. However, this is obviously not optimal and a clear hack.
Upvotes: 42
Views: 39088
Reputation: 1803
The space is there because of the UITableView
's tableHeaderView
property. When the the tableHeaderView
property is nil
Apple defaults a view. So the way around this is to create an empty view with a height greater than 0
. Setting this overrides the default view thereby removing the unwanted space.
This can be done in a Storyboard by dragging a view to the top of a tableView
and then setting the height of the view to a value of 1
or greater.
Or it can be done programmatically with the following code:
Objective-C:
CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];
Swift:
var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)
As others have noted you can use this same solution for footers.
See the Documentation for more details on the tableHeaderView
property.
Thanks to @liushuaikobe for verifying using the least positive normal number works.
Upvotes: 136
Reputation: 147
Follow these steps to save your day.
Select grouped attributes from the storyboard.
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
If you are using ios 15 do this also —
if #available(iOS 15.0, *){
self.tableViewSavedRecent.sectionHeaderTopPadding = 0.0
}
Upvotes: 9
Reputation: 893
If you are using a TableHeaderView with an insetGroup styled UITableView you can do the following:
class CustomTableHeaderView: UIView {
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
--- do your header view setup here. ---
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 1
Reputation: 1693
Use it in the viewDidLoad()
method.
tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
Upvotes: 13
Reputation: 1984
None of above solution worked for me
In my case these setting was set to manual i just changed to Automatic
Upvotes: 1
Reputation: 257
I had to combine both delegate functions for it to work:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: .leastNonzeroMagnitude))
}
Upvotes: 3
Reputation: 1101
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
Below location icon is table with top spacing zero.
Upvotes: 4
Reputation: 477
Answer in Swift 4
If the table view is selected in interface builder and in the attributes inspector the style "Grouped" is selected, enter the following code in your view controller to fix the extra header space issue.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
Upvotes: 18
Reputation: 288
For iOS 11.0+
tableView.contentInsetAdjustmentBehavior = .never
Upvotes: 6
Reputation: 41
You need to set footer too
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectZero];
}
Upvotes: 4
Reputation: 571
If the style of your tableView is UITableViewStyleGrouped
, then you have to pay attention to the delegate of the height of SectionHeader or SectionFooter, cause this needs to be implemented right under this case.
The return value should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it needs to be 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;
}
Make sure you implemented these two methods, and the value is right, and the top margin will be fixed.
Upvotes: 5
Reputation: 10328
Single line solution:
Objective-C
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
Swift
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double(FLT_MIN)))
Upvotes: 10