Search code examples
iosobjective-cuitableviewautolayoutuistoryboard

Can't center views in tableHeaderView with auto layout and storyboards


I can't center a view that has been placed as a subview of a UITableView's tableHeaderView in a storyboard using auto layout.

In a storyboard, I have a UITableView. I dragged a UIView (the red view) to the top, released, and it created a table header view automatically. I then dragged and dropped another UIView (the yellow view) on top of the table header view, resized, and applied some constraints to ensure it stays centered:

enter image description here

When I run the app on the simulator, here's what I get:

enter image description here

The yellow view is obviously not centered. However, the "Filter" button at the bottom is.

I know it's tricky to get the height right using auto layout and storyboards and table header views (and you can see that the height of the red view is definitely incorrect), but at this point, I'm just trying to solve for horizontally centering my yellow view.

Am I able to set this all up in my storyboard without having to configure my constraints in code?


Solution

  • Make sure that your UITableView has the leading, trailing, bottom, top constraints set up against its superview.