title: How to let child view controller add UISearchBar to parent's UINavigationController?
body: <p>How to let child view controller modify navigation controller?</p>
  
  <p>I am faced with this problem right now, and have not been able to find a solution that works, let alone one that works well.</p>
  
  <p>Basically, right now I have a <code>UINavigationController</code> that contains a <code>UITableViewController</code> with a search bar in the navigation bar. So far so good - everything works well.</p>
  
  <p>However, I would like to show some custom views between the navigation bar and the top of the table view at times. To do this, I figured it would be best to create another view controller (let's call it the <code>ContainerViewController</code>), which is presented by the navigation controller. This container view controller holds the table view controller as a child, and can insert any custom view it wishes to. The view controller hierarchy should look like this:</p>
  
  <pre><code>UINavigationController
      ContainerViewController
          UITableViewController (with UISearchDisplayController)
  </code></pre>
  
  <p>Now I am faced with a problem, since the search bar of the <code>UISearchDisplayController</code> should be displayed in the navigation bar (by calling <code>didSetupSearchDisplayController</code>), but I obviously want all logic pertaining to it kept in the <code>UITableViewController</code>. I need the container view controller to sort of act as a go-between here. I have tried to figure this out, but haven't managed a solution yet.</p>
  
  <p>Here is how I instantiate the table view controller and add it as a child in the container view controller.</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      // Load view controller from storyboard
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
      self.contentViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyListViewController"];
      self.contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
  
      // Add its view to the content view
      [self addChildViewController:self.contentViewController];
      [self.container addSubview:self.contentViewController.view];
      [self.contentViewController didMoveToParentViewController:self];
  
      // Set up constraints
      [self.contentViewController.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  }
  </code></pre>
  
  <p>In the child view controller, I have noticed that <code>self.navigationController</code> is nil in <code>viewDidLoad</code> but not in <code>didMoveToParentViewController</code>. So the following works:</p>
  
  <pre><code>- (void)didMoveToParentViewController:(UIViewController *)parent {
      // Hide separator below navigation bar
      self.navigationController.navigationBar.clipsToBounds = YES;
  }
  </code></pre>
  
  <p>But I cannot set the search bar to display in the navigation bar, nor can I set the navigation bar title (with <code>self.title = ...</code>) from within the child view controller.</p>
  
  <p>Would appreciate any help.</p>
  
  <p><strong>EDIT:</strong> Here is some code I use in the <code>UITableViewController</code> (the contained view) to create the search display controller:</p>
  
  <pre><code>UISearchBar *searchBar = [[UISearchBar alloc] init];
  self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  
  // Show search bar in navigation bar (instead of in its own bar underneath)
  self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
  
  // Set self as delegate to search display controller and its table view
  self.searchDisplayController.delegate = self;
  self.searchDisplayController.searchResultsDelegate = self;
  </code></pre>
  
  <p>I have tried doing this both in <code>viewDidLoad</code>, where the <code>self.navigationController</code> is nil, and in <code>didMoveToParentViewController</code>, where the navigation controller exists, but it does not seem to matter. My guess is that the <code>displaysSearchBarInNavigationBar</code> magic happens <em>after the child view is  instantiated but before it is added as a child view</em> (and thus gets access to the navigation controller).</p>