Reputation: 19979
I'm picking up some iOS programming and am trying to put a UITableView into a storyboard. Unfortunately, I am trying to put the content at the top of the view but it is putting in some space. I have tried to adjust the values in the inspector for View -> Mode but this doesn't seem to have any effect.
I have made the background green and put a border color to show the issue. I'm not a sophisticasted iOS dev, so I'd assume that this is the simplest solution and not something complex. How do I make the contents of the table view sit flush with the top? I've seen this Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7 but not sure if it's related.
thx for any help
Updated with changes and screen shot of properties for this table view
Upvotes: 39
Views: 43438
Reputation: 3415
For iOS 15.0+,
use this for removing extra padding at the top
if #available(iOS 15.0, *){
self.tableView.sectionHeaderTopPadding = 0.0
}
Upvotes: 0
Reputation: 1
This worked for me in swift:
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
Upvotes: 0
Reputation: 596
Everybody keeps talking about this self.automaticallyAdjustsScrollViewInsets
option.
However this did not work for me at all. What did work for me, was setting the "Content Insets" property to Never
.
Such an annoying problem.
Upvotes: 9
Reputation: 27
Maybe you have more than one parent navigation controller? If that's the case, uncheck other parent navigation controllers' "Show Navigation Bar".
Upvotes: 0
Reputation: 37300
Yes, that other question is very much related. UITableViewStyleGrouped
divides each section into a "group" by inserting that extra padding…similar to what it did pre-iOS7, but clear instead of colored by default, and just at the top instead of all the way around. If you don't want the padding by default, use UITableViewStylePlain
.
Otherwise, if you need to keep the style the same, do what this other posted from that link recommended and change the content inset:
self.tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);
Or do what this poster suggested and set the tableHeaderView
's height
to .-1
, i.e. nearly 0
:
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
Upvotes: 66
Reputation: 2332
This is how it can be fixed easily through Storyboard:
Select Table View > Size Inspector > Content Insets: Never
Upvotes: 1
Reputation: 135
in swift 3.0, i hope will help you
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
Upvotes: 1
Reputation: 1213
It is very easy and worked for me perfectly. Select a controller in Story Board in which you placed UITableView.
YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets.
Make sure you select UIViewController not UITableView.
Upvotes: 5
Reputation: 213
try this....
self.tableView.contentInset = UIEdgeInsetsMake( 20, 20 , 0, 0)
Upvotes: 1
Reputation: 1473
Cause of this issue:-
In ViewDidLoad:-
self.edgesForExtendedLayout = UIRectEdge.None
self.automaticallyAdjustsScrollViewInsets = false
No Need For Something Like This :-
self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)
In heightForHeaderInSection
delegate:-
if section == 0
{
return 1
}
else
{
return 40; // your other headers height value
}
In viewForHeaderInSection
delegate :-
if section == 0
{
// Note CGFloat.min for swift
// For Objective-c CGFLOAT_MIN
let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min))
return headerView
}
else
{
// Construct your other headers here
}
Upvotes: 0
Reputation: 7152
In Swift, you just need to set the below property to false.
self.automaticallyAdjustsScrollViewInsets = false
Upvotes: 6
Reputation: 14292
Swift :
override func viewWillAppear(animated: Bool) {
self.edgesForExtendedLayout = UIRectEdge.None
OR
self.moduleListTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
OR
self.automaticallyAdjustsScrollViewInsets = false
}
Upvotes: 1
Reputation: 1588
Go to the attributes inspector of the View Controller by selecting the xib or the controller in Storyboard. Uncheck the Adjust Scroll View Insets in Layout. It will solve the problem
Upvotes: 58