0

I have a UIView with a UITableView right below it. The tableview has a custom header which hold a UISearchbar and a UISegmententedController. The top view and tableview are constrained to each other and to the superview. If I hide the top view, the header of the tableview gets stretched.

Top view visible:

Top view is green and directly under it is the tableView

Top view hidden (height constraint set to 0):

enter image description here

My expectation would be that when I hide the top view my tableview content should scroll up, but instead of repositioning the cells, the tableview stretches out the header view.

Thanks for any help!

Kevin
  • 483
  • 3
  • 11

2 Answers2

0

I'm not sure why but if the custom tableView header is created from a xib then it gets messed up. But if the view is created programmatically it works. I don't know why but to fix the problem just needed to create the tableview header programmatically

Bad:

if let headerView = Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?[0] as? HeaderView { 
  tableview.tableHeaderView = headerView
}

Good:

tableview.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 80))
Kevin
  • 483
  • 3
  • 11
  • Apparently the problem had to do with the autoresizing mask. Not letting the view autoresize for the height seems too have fixed it as well – Kevin May 30 '17 at 14:39
0

If you want to make the tableView scroll up, I would recommend getting the height constraint of the top view and reducing it to 0. You can update the height constraint in a UIView.animate block to have a smoother transition when showing and hiding the top view.

Refer here for more details: AutoLayout with hidden UIViews?

Brian Endo
  • 46
  • 5