0

Ive created just a new xcode project without any coding. I just worked in the storyboard. There I have a Navigation Controller. My RootViewController has a TableView with Static cells in 3 sections (Grouped). The Section headers I deleted. My next step was to change the distances between the sections. For top I said 4 and for bottom 4, too. It looks now like this:

Current Storyboard

Now my problem: The size between the first section and the top is really big (I think 32 because thats the automatic distance between the sections). THe only place I found something was the size inspector - Content Insets. This is set to automatic. With no option I can put some values in myself. If I choose never there, the section got displayed under the Root View Controller header. My size inspector looks like this:

Size inspector

Can anyone tell me how I can reduce the size between viewController top and first section? Exactly I want to have a size of 16 between first section and the header of the Root View Controller.

And is there a way to do a change for the complete project so that also upcoming navigation controllers will have their tableview with a size of 16 between first section and the header?

What Ive tried: I found this question Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7 Here someone asked for tableViews in general. The solutions I readed there which didnt worked for me:

  • Turn Adjust scroll view insets off. I tried this for the root as well as the navigation controller. I also tried to solve the problem in code.
  • Set edgesForExtendedLayout to none in viewDidLoad - nothing changed.
  • Set the header just for first section to 1.
  • self.navigationController?.navigationBar.isTranslucent = true changed nothing

Half correct: One solution was changing the contenInset manually in code. I dont like this because I would have to type the 16 points hard in my code. I dont know there the header of 32 comes from. So I dont know if the 16 points will stay forever. So I want a safer solution for future as well. THis was the hard solution:

self.tableView.contentInset = UIEdgeInsetsMake(-16, 0, 0, 0)
Sonius
  • 1,597
  • 3
  • 14
  • 33

2 Answers2

1

That top space comes from grouped style tableView header. You can add your own table header view at top of the table, set its color to "Group Table View Background Color", and set its height to what you want (16).

enter image description here

Predrag Samardzic
  • 2,661
  • 15
  • 18
1

iOS11 Pure code Solution

Anyone looking to do this in pure code and remove the 35px empty space needs to add a tableHeaderView to their tableView - it appears having it as nil just sets it as default sizing of 35px

i.e.

tableView.tableHeaderView = tableHeaderView

var tableHeaderView:UIView = {
    let view = UIView()
    view.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: CGFloat.leastNormalMagnitude)
    return view
}()

Note device width would be your bounds/view width and height is NOT 0 - but CGFloat.leastNormalMagnitude

Nick Wood
  • 680
  • 6
  • 10
  • Did it in the meanwhile like you. I had many tables in my project so that I was able to do a extension with this solution and dont have to look up all storyboards for changes – Sonius Mar 08 '18 at 10:06