title: UITextField within UISearchBar in iOS 7
body: <p>I am trying to accomplish the same look of my UISearchBar with a TextField within it, as in my iOS 6 app. I have tried to code it in several ways and not yet been successful. The problem is, I am not able to change the TextField's frame in any way since iOS 7. The result is, my TextField takes all the space in the NavigationBar and overrides the UIBarButtonItem (menu button) to the right. See pictures below: </p>
  
  <p><a href="https://i.stack.imgur.com/xMrKd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xMrKd.png" alt="Screenshots"></a>
  </p>
  
  <p><strong><em>iOS 6 code:</em></strong> <em>This is how I coded it in iOS 6, where I could set the TextFields frame to whatever I liked!</em></p>
  
  <pre><code>UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject];
  [sbTextField removeFromSuperview];
  
  CGRect rect = searchBar.frame;
  rect.size.height = 32;
  rect.size.width = 210;
  sbTextField.frame = rect;
  
  [sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
  
  
  UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc]  initWithCustomView:sbTextField];
  
  [[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];
  </code></pre>
  
  <p><strong>The result from the code above in iOS 7:</strong> ![iOS 7 look]</p>
  
  <p><strong><em>iOS 7 code:</em></strong> <em>The difference in iOS 7, is that you need to use subViews in order to add the UITextField to the UISearchBar/UINavigationBar. By doing this I have not yet been able to change its frame. It currently overlaps the menuButton to the right which can be seen in the picture below this code...</em></p>
  
  <pre><code>UITextField* sbTextField;
  CGRect rect = subView.frame;
  rect.size.height = 32;
  rect.size.width = 115;
  
  for (UIView *subView in self.searchBar.subviews){
      for (UIView *ndLeveSubView in subView.subviews){
  
          if ([ndLeveSubView isKindOfClass:[UITextField class]])
          {
  
              sbTextField = (UITextField *)ndLeveSubView;
              sbTextField.backgroundColor =[UIColor whiteColor];
              UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
              sbTextField.frame = rect;
              self.navigationItem.leftBarButtonItem = searchBarNavigationItem;
              self.navigationItem.rightBarButtonItem =  menuButton;
              [sbTextField removeFromSuperview];
  
               break;
          }
  
      }
  
   }
   [self.searchBar reloadInputViews];
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/onDO4.png" alt="enter image description here">
  SO...Is it possible to change a subView's frame (TextField) in any way ? :(</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><em>The answer is kinda lame. In order to make the code work in ios7 with a button to the right of the TextField, the TextField must be set as the titleView of the navigationBar. Which was not the case in ios 6. But there will be other glitches and it is not recommended to use TextField within searchBars in iOS7. Use searchDispalyController instead. Se my answer below</em></p>
  
  <pre><code> self.navigationItem.titleView = sbTextField;
  </code></pre>