I have a UITableView and a navigation bar in a single screen of my app. I want to navigation bar to be scrolled together with the rest of the table.
Note: I am currently implementing a UITableView that is added as a subview of a uiviewcontroller which is in turn part of a navigation controller
Can anyone advise me on how to do this?
The only way a navigation bar can be scrolled with the other cells of table-view is if the navigation bar is a part of the table view or is a subview of a larger scroll-view (in addition to the table-view's subview). check out this question: How can I make the navigation bar scroll along with my webpage?. Make a larger scroll-view and add the table-view and navigation-bar to this scroll-view. If you want to make the navigation bar a part of the table-view, you'll need to make a custom-cell for it, which will incur a lot more hassle!
In the following answer I'm hiding the real nav bar and replacing it with a fake bar. If you want to use the real nav bar instead, the code is still valid, just restore the original position of the bar in viewWillDisappear.
Hide the navigation controller bar:
Add a top bar as a subview of the current controller view.
At this point you have a headerView and a tableView below. Save their original frames:
Add this in the view controller, which is also your table delegate:
This checks the table height and moves top bar as the table view scrolls. Since the tableView is moving over the are previously filled by the top bar, I'm also enlarging the size of the table view.
edit: just uploaded an implementation in github with a top bar of 80 pixels and kHiddenPixels=35:
A possible enhancement: Right now, as the first row of the tableView scrolls under the top bar, the top bar scrolls below the clock bar. It would look more natural if the tableView and top bar scroll together until the top bar is gone, and then let the 1st row start scrolling under the top bar. Not sure how to do this, maybe with a pan gesture recognizer and disabling table scroll until the bar is hidden.