0

I have a UITableViewController embedded on a UINavigationController. This tableView is an instance of NSFetchedResultsController. I need to add a Toolbar between the NavigationController's top bar and the TableViewController, but I can't figure out how to do it (I don't even know if it's possible). I want to do something like Apple did with their WWDC App (except that they don't have the TableViewController embedded in the NavigationController). I need to have some controls on the bar to drive the NSFetchedResultsController.

Some people suggested to people with similar problems to use a UITableView instead of a TVC, but I do need to have a TVC as an instance of NSFetchedResultsController.

Any ideas on how to accomplish this? Would I have to do it programmatically? If so, how?

Btw, I'm targeting iOS6+ with storyboards and ARC.

Apple's WWDC App

Eric
  • 3,301
  • 4
  • 33
  • 39

6 Answers6

6

The approach I prefer is to use a UIViewController at the outer level, containing your toolbar and a container view that holds the table. Then build your table in a separate UITableViewController and wire it into the container view using an embed segue. Overall, I think this makes the code more modular and easier to follow because the high-level structure is laid out in the storyboard.

The steps to use an embed segue are as follows:

  1. Control-drag from the container view to the view controller you want to embed and select the "Embed" option.
  2. Give the embed segue an identifier in the attributes inspector.
  3. Configure the table view controller in the parent's prepareForSegue method, checking for your segue's identifier.

There is an example of this in my VCollectionViewGridLayout library. Take a look at the Sort & Filter example project.

Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
2

Technical Note TN2154: UIScrollView And Autolayout provides another solution:

Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

That is, even if a scroll view (such as a table view) modifies the subview's frame, the auto layout engine will reset it on the next layout pass.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks @Aaron. I'm just trying to get this working, but the subview is still scrolling along with the table view. Are you able to provide any example code? Thanks! – Chris Nolet Oct 15 '14 at 04:59
  • I couldn't get this working with a UITableViewController (without a container view) since there's no superview for `self.tableView`. Creating a constraint between the UINavigationController and floating view doesn't work either, since they're in different view hierarchies and that crashes the app. – Chris Nolet Oct 15 '14 at 18:43
  • 1
    `self.tableView` does have a superview, although it may not be set yet in `viewDidLoad`. Create a basic `UITableViewController` and you'll see `self.tableView.superview` is not `nil` once `viewWillAppear:` is called. – Aaron Brager Oct 15 '14 at 19:19
  • Ahh, got it :) It works! I do have to disable the constrains on `viewWillDisappear` to stop the view from vanishing when the superview is removed again: `self.balanceView.translatesAutoresizingMaskIntoConstraints = YES;` – Chris Nolet Oct 15 '14 at 19:55
1

you need use UIViewController, then add tool bar and tableView instance of NSFetchedResultsController class inside it in storyboard

crz
  • 438
  • 5
  • 18
0

You can make a topBar of any UIView and then pass it as the tableHeaderView. It may help you.

Divyam shukla
  • 2,038
  • 14
  • 18
0

Use UIViewController instead of just UITableViewController where you can easily place other controls apart from just a UITableView.

Hope this helps.

iphonic
  • 12,615
  • 7
  • 60
  • 107
0

You can use the UITableViewController (keep the niceties such as UIRefreshControl support and keyboard avoidance). You just have to embed your toolbar in a plain view and place that in your tableHeaderView. Then implement this scroll view delegate method to lock.

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect rect = self.toolbarContainerView.frame;
    rect.origin.y = MIN(0,scrollView.contentOffset.y + scrollView.contentInset.top);
    self.toolbarContainerView.frame = rect;
}

Note that if you also use section header you will have to send those views behind your tableHeaderView otherwise they will float over the tableHeaderView.

Steve Moser
  • 7,647
  • 5
  • 55
  • 94