Search code examples
iosswiftuitableviewxcode6.4

Lock UIToolbar to Top of UITableViewController


There are many questions like mine both in Objective-c and Swift that ask how to lock a UIToolbar to the top of a UITableViewController, and I will add to that list. However, my question is unanswered by the others. I need to lock it to the top of the view controller so that it is not considered a cell by the program, and so it doesn't move down, but is locked at the top instead.

All other answers that I found want me to either

(a) Embed the UITableViewController in a UINavigationViewController

or

(b) To use a UIViewController with a UITableView inside it


I do not want to do either one of these.
I want to keep a UITableViewController––not to replace it with a UIViewController and UITableView––and I do not want to use a UINavigationViewController––I am trying to not do that because it causes a plethora of issues in the rest of the app (which can be fixed but I don't want to have to do that).


Solution

  • What you are asking for is not reasonably possible. The root view of UITableViewController is a UIScrollView. Every view you add as a subview in a UITavleViewController must be part of that scrollview and thus will scroll with it.

    To make it work you must take one of the options you mentioned (moving to a UIViewController with a table view as a subview is likely to cause the least disruption in your app).

    There are a couple of unreasonable solutions I can think of: - implement the scrollviewDidScroll delegate method in you UITableViewController and constantly reposition tour toolbar within the scrollview to make it appear to always be at the top (the toolbar will probably jitter around and look terrible) - create a new UIWindow on top of your entire app that hold the toolbar (don't do this, it is a terrible idea).