title: When adding a Search Bar and Search Display Controller to a complex tableHeaderView, The UISearchBar disappears when it gains focus
body: <p>I am using a single ViewController and an associated NIB, no storyboard. I'm ready to pull my hair out.</p>
  
  <h2>The Issue</h2>
  
  <p>Notice in the video after focusing, a tiny bit of the field is visible in the top left corner, only to disappear completely after clicking on the shaded area over the UITableView. When going into the view hierarchy, it shows that the searchBar is being moved out of its original hierarchal position.</p>
  
  <p><a href="https://vid.me/5054" rel="nofollow">This video</a></p>
  
  <h2>The NIB</h2>
  
  <p><a href="http://i.imgur.com/ZJcLbkl.png" rel="nofollow">Screenshot of the NIB, showing the structure of the tableHeaderView</a></p>
  
  <h2>The Code</h2>
  
  <pre><code>import UIKit 
  class ViewController: UIViewController, UITableViewDataSource, 
  UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate {
  
  @IBOutlet weak var bigLabel: UILabel!
  @IBOutlet weak var buttonGroupSegment: UISegmentedControl!
  @IBOutlet var tableViewHeader: UIView!
  @IBOutlet weak var tableView: UITableView!
  @IBOutlet var searchBar: UISearchBar!
  
  // MARK: VIEW CONTROLLER
  override init() {
      super.init(nibName: "ViewController", bundle: nil)
  }
  
  required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
      self.initTableView();
  }
  
  override func viewDidLoad() {
      super.viewDidLoad();
      self.initLabelStyling();
      // Do any additional setup after loading the view, typically from a nib.
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  // MARK: TABLE VIEW HEADER
  func initTableViewHeader() -&gt; Void{
      self.tableView.tableHeaderView = self.tableViewHeader;
  
      self.initSearchView();
      self.initSegmentedControl();
  }
  
  // MARK: THE SEGMENTED CONTROL
  func initSegmentedControl() -&gt; Void{
      let font = UIFont(name: "HelveticaNeue", size: 10);
      let color = UIColor.whiteColor();
      let lighterColor = color.colorWithAlphaComponent(0.5);
      self.buttonGroupSegment.tintColor = UIColor.clearColor();
  self.buttonGroupSegment.setTitleTextAttributes([NSForegroundColorAttributeName : lighterColor, NSFontAttributeName: font!, NSKernAttributeName: 0.7], forState: UIControlState.Normal)
  
      self.buttonGroupSegment.setTitleTextAttributes([NSForegroundColorAttributeName : color, NSFontAttributeName: font!], forState: UIControlState.Selected);
  
      // Change the case of all the titles
      let c = self.buttonGroupSegment.subviews.count;
      for i in 0..&lt;c{
          if let title = self.buttonGroupSegment.titleForSegmentAtIndex(i){
              self.buttonGroupSegment.setTitle(title.uppercaseString, forSegmentAtIndex: i);
          }
      }
  
  }
  
  // MARK: SEARCH VIEW
  func initSearchView() -&gt; Void{
      let searchBar = self.searchDisplayController!.searchBar;
      searchBar.backgroundColor = UIColor.clearColor();
      searchBar.backgroundImage = UIImage();
      searchBar.barTintColor = UIColor.clearColor();
      searchBar.tintColor = UIColor.whiteColor()
  
      let numViews = searchBar.subviews[0].subviews.count;
      var searchField: UITextField?
  
      for i in 0..&lt;numViews{
          if searchBar.subviews[0].subviews[i].isKindOfClass(UITextField.self) {
              searchField = searchBar.subviews[0].subviews[i] as? UITextField;
          }
      }
  
      if searchField != nil{
  
          // Fonts and colors
          let font = UIFont(name: "HelveticaNeue", size: 12);
          let textColor = UIColor.whiteColor();
          searchField!.font = font;
          searchField!.textColor = textColor;
          if searchField!.respondsToSelector("setAttributedPlaceholder:"){
              if let placeholder = searchField!.placeholder{
                  let placeHolderColor = UIColor(red: 100, green: 100, blue: 100, alpha: 0.5);
                  let attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSForegroundColorAttributeName : placeHolderColor, NSFontAttributeName: font!]);
                  searchField!.attributedPlaceholder = attributedPlaceholder;
              }
          }
          searchField!.backgroundColor = UIColor(red: 100, green: 100, blue: 100, alpha: 0.2);
          var searchIcon: UIImageView? = searchField?.leftView as? UIImageView;
          searchIcon!.image = searchIcon!.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
          searchIcon?.tintColor = UIColor.whiteColor();
  
          var clearIcon: UIImageView? = searchField?.rightView as? UIImageView;
          if clearIcon != nil{
              clearIcon!.image = clearIcon!.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
              clearIcon!.tintColor = UIColor.whiteColor();
          }
      }
  
  }
  
  // MARK: THE SEARCH VIEW DELEGATE METHODS
  func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
  
  }
  
  func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -&gt; Bool {
      self.filterContentForSearchText(searchString, scope:"");
      return true;
  }
  
  func filterContentForSearchText(searchText: NSString, scope: String) -&gt; Void{
      self.dataFiltered = self.data.filter({(pet: String) -&gt; Bool in
          let stringMatch = pet.rangeOfString(searchText)
          return stringMatch != nil;
      })
  }
  
  // MARK: TABLE VIEW
  let data = ["Cat","Dog","Bird","Fish","Ferret","Rat","Hamster","Chicken","Goat","Pig","Donkey","Monkey", "Rabbit", "Fox", "Snake", "Frog", "Spider","Rooster","Aligator","Pocupine","Squirrel","Duck","Turtle","Lizard","Lama"];
  var dataFiltered: [String] = [];
  let cellClass: String = "Cell";
  
  func initTableView() -&gt; Void{
      self.initTableViewHeader();
      self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellClass);
  
  
  // MARK: TABLE VIEW DELEGATES AND DATA SOURCE METHODS
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int{
  
      if tableView == self.searchDisplayController!.searchResultsTableView{
          return self.dataFiltered.count
      }else{
          return self.data.count;
      }
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell{
  
      var cell: UITableViewCell?
  
      cell = self.tableView.dequeueReusableCellWithIdentifier(cellClass) as? UITableViewCell;
  
      if (cell == nil){
          cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.cellClass)
      }
  
      if tableView == self.searchDisplayController!.searchResultsTableView{
          cell?.textLabel?.text = self.dataFiltered[indexPath.row]
      }else{
          cell?.textLabel?.text = self.data[indexPath.row];
      }
  
      return cell!;
  }
  
  // MARK: STYLE THE LABEL (ADD A LINE)
  func initLabelStyling()-&gt;Void{
      let v = UIView(frame: CGRectMake(0, 0, self.bigLabel.frame.width, 1));
      v.backgroundColor = UIColor(red: 100, green: 100, blue: 100, alpha: 0.2);
      self.bigLabel.addSubview(v)
  }
  }
  </code></pre>