0

When I create table view with custom table view, there is empty space between Navigation Bar and the top of Table View.

It looks: enter image description here

I've added constraint to as equal of the view.topAnchor. I don't know why is this happening.

How to clip the Table View to the top?

Thank you in advance.

IvanPN
  • 23
  • 4

2 Answers2

1

Make sure that you don’t have any header added. If you are using a nib/storyboard checkout the ViewController design -> Select tableview -> set Footer height & Header height to 0.

If you are using code, checkout this example on how to remove the header using code - How to hide first section header in UITableView (grouped style)

By default it will take 28 that's why it shows blank on top.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
}

set above code and make sure delegate method will call.

jatin fl
  • 157
  • 6
  • sorry here i post as answer because i can't do comment. – jatin fl Jun 17 '21 at 06:34
  • Thank you for the clue. But, I created the table view programmatically. How to set those property programmatically? – IvanPN Jun 17 '21 at 06:53
  • 1
    Unfortunately, your code doesn't work on mine. I just found the workaround in your link you shared. I put this code below to my viewDidLoad and it overcomes the issue: `tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: tableView.bounds.size.width, height: 0.01))` – IvanPN Jun 17 '21 at 07:13
0

Try this trick, add to intere view a blank UIView... In viewDidLoad:

view.addSubview(UIView())

I ignore the reason but this breaks the link between your the UIScrollView and the nav bar... This probably solve your issue.

Fabio
  • 5,432
  • 4
  • 22
  • 24