0

I created header for tableview(!not for the section) in story board with auto layout.

I want to have ability to show/hide it.

How can I do it?

I tried this code:

self.tableView.tableHeaderView.hidden = !flag;

CGRect frame = self.tableView.tableHeaderView.frame;
frame.size.height = flag? k_HEIGHT_TableViewHeader : 0;
self.tableView.tableHeaderView.frame = frame;

It works , But it gives me a lot of auto layout errors.

Also I can set it to nil. If so, how can I load header from storyboard?

Mat Demon
  • 197
  • 1
  • 9

2 Answers2

0

Have you tried using [tableview reloadData] when you decide to hide/ show?

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • yes, it hide/show it but if it hides the frame did not changes, so I have clear space instead my header – Mat Demon Aug 24 '15 at 19:47
  • at that point you would want to do the code to move your tableview back to the top. This can be done by making an IBOutlet NSLayoutConstraint *topConstraint and connecting it to your 'top space to Superview' constraint. Update that topConstraint to 0 when you hide and whatever you need to (like 30 pixels) when you show. – MSU_Bulldog Aug 24 '15 at 20:33
0

Because you created it with auto layout, rather than changing the frame, you should link the constraint for the height on your tableHeaderView from the storyboard to your class. Then when you want to collapse it, set the constraint.constant = 0 and expand it again by setting constraint.constant = k_HEIGHT_TableViewHeader. You will need to call layoutIfNeeded on the view afterwards as well.

This change can also be easily animated by wrapping the layoutIfNeeded in a UIView animation block

[UIView animateWithDuration:0.5 animations:^{[self.view layoutIfNeeded];}];

Auto Layout Animation Reference

Community
  • 1
  • 1
esthepiking
  • 1,647
  • 1
  • 16
  • 27