1

I'm trying to add a custom subview to a UITableViewController that doesn't move when the user scrolls.

Is it possible to add such a view?

I'm using a UITableViewController. I would switch to a UIViewController and add a UITableView, but the code relies on the UITableViewController's refreshControl. Adding a container view and having two controllers seems a bit much for such a simple task!

I've also tried adding the content to the UINavigationController view, but unfortunately it doesn't animate smoothly when the view controller appears and disappears.

Is there any way to add a fixed subview to a UITableView?

Edit:

Adding a container view and having two controllers (with an embed segue) is difficult for this project, since I'm updating an older code base and there is a lot of legacy code that depends on the main controller being a UITableViewController. Is there any way to achieve this without an embed segue / two view controllers?

Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
  • 1
    See also the similar [Disable tableHeaderView -- not to be confused with section header -- scrolling](http://stackoverflow.com/q/18563172) – jscs Oct 14 '14 at 21:47
  • 1
    Just added the [answer I was typing here to the duped question](http://stackoverflow.com/a/26370964/1445366). – Aaron Brager Oct 14 '14 at 21:49
  • This is great! Thanks for nominating the duplicate questions. My only concern is that a lot of these answers say "add a container view"; it might be good to have a canonical solution which doesn't require a container view and embed segue. Aaron has a good answer and this answer (http://stackoverflow.com/a/18565273/746890) is pretty good. – Chris Nolet Oct 15 '14 at 02:23
  • Possible duplicate of [How to put buttons over UITableView which won't scroll with table in iOS](http://stackoverflow.com/questions/14689805/how-to-put-buttons-over-uitableview-which-wont-scroll-with-table-in-ios) – Peter Lapisu Oct 20 '16 at 13:57
  • There are a few questions on SO that deal with similar issues, but I haven't found any prior questions that specifically exclude switching to a `UIViewController`. I think it's good to have a canonical solution for that. – Chris Nolet Oct 20 '16 at 18:12

1 Answers1

3

Unfortunately creating a container view for a UITableViewController inside of a UIViewController isn't feasible in this case. That is a great solution if you're early enough in development to arrange things properly.

The best solution I've found, is to transform the floating view's y position on scrollViewDidScroll as per this answer:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.header.transform = CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y);
}

I have seen other answers which modify the floating view's frame, but I think transforming is probably more appropriate.

As an interesting aside, I was able to create a floating view that 'dismisses' when the user scrolls down, but 'floats' when the user pulls to refresh by checking the table view offset before applying the transform.

Community
  • 1
  • 1
Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
  • 1
    Thanks! In case you have insets, you'll need to take account of that: `CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y + self.tableView.contentInset.top);` – Toland Hon Feb 26 '15 at 00:46
  • put that in "viewDidLayoutSubviews", which is called for every frame when scrolling, e.g. `override func viewDidLayoutSubviews() { noDataLabel?.transform = CGAffineTransform(translationX: 0, y: tableView.contentOffset.y) }` – Ron Aug 04 '16 at 13:26