title: Put search bar in nav bar programmatically
body: <p>I am trying to place a search bar in a navigation bar using the following code:</p>
  
  <pre><code>  UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
      searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
      searchBarView.autoresizingMask = 0;
      searchBar.delegate = self;
      [searchBarView addSubview:searchBar];
      self.navigationItem.titleView = searchBarView;
  </code></pre>
  
  <p>It does place the search bar in the nav bar but does not cover the buttons on either side.  It also does not have a cancel option.</p>
  
  <p><a href="https://i.stack.imgur.com/22mZo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/22mZo.png" alt="enter image description here"></a></p>
  
  <p>I would prefer to have it look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/5w7Ee.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5w7Ee.jpg" alt="enter image description here"></a></p>
  
  <p>Can anyone suggest how to adjust the above code to do that?</p>
  
  <p>Thanks in advance for any suggestions. </p>