title: Crash on search bar tap
body: <p>App crashes when search bar text field is tapped in UIViewController  </p>
  
  <p>My ViewController looks like this -</p>
  
  <pre><code>class DetailViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate {
  
  let testBaseUrl = "https://maps.googleapis.com/maps/api/geocode/json?"
  
  var searchBar = UISearchBar()
  var searchImageView   : UIImageView!
  var mapView: GMSMapView!
  var markers = [GMSMarker]()
  var cameraPosition: GMSCameraPosition!
  var searchAddress: String!
  
  var mapButton = UIBarButtonItem()
  var listButton = UIBarButtonItem()
  var filterButton = UIBarButtonItem()
  var arrayOfButtons = [AnyObject]()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      cameraPosition = GMSCameraPosition.cameraWithLatitude(-37.809487,
          longitude:144.965699, zoom:17.5, bearing:30, viewingAngle:40)
      mapView = GMSMapView.mapWithFrame(CGRectZero, camera:cameraPosition)
  
      mapView.delegate = self
      self.view = mapView
  
  }
  
  override func viewWillAppear(animated: Bool) {
      setUISearchView()
  
  }
  
  //MARK: SearchBar Methods
  
  func setUISearchView(){
  
      let searchImage = UIImage(named: "search")!
  
      searchImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height))
      searchImageView.image = searchImage
  
      let rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "showSearchBar")
      searchImageView.addGestureRecognizer(rightGestureRecognizer)
  
      navigationItem.rightBarButtonItem?.customView = searchImageView
  
  }
  
  func setUICancelView(){
  
      let searchImage = UIImage(named: "cancel")!
  
      searchImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height))
      searchImageView.image = searchImage
  
      let rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideSearchBar")
      searchImageView.addGestureRecognizer(rightGestureRecognizer)
  
      navigationItem.rightBarButtonItem?.customView = searchImageView
  
  }
  
  
  func searchButtonPressed(sender: UIBarButtonItem) {
      showSearchBar()
  }
  
  func showSearchBar() {
  
      searchBar.delegate = self
      searchBar.searchBarStyle = UISearchBarStyle.Minimal
  
      let textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
  
      textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
  
      UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.redColor()
  
      searchBar.alpha = 0
      navigationItem.titleView = searchBar
  
      UIView.animateWithDuration(0.5, animations: {
          self.searchBar.alpha = 1
          self.setUICancelView()
          }, completion: { finished in
              self.searchBar.becomeFirstResponder()
      })
  }
  
  func hideSearchBar() {
      setSearchBarTitle(searchAddress!)
  
      searchImageView.alpha = 0
  
  
      UIView.animateWithDuration(0.3, animations: {
  
          self.searchBar.hidden =  false
          self.searchBar.alpha = 0
          self.setUISearchView()
          }, completion: { finished in
              self.searchBar.resignFirstResponder()
  
      })
  }
  
  //MARK: UISearchBarDelegate
  func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
       print("search is working ")
  }
  
  func searchBarCancelButtonClicked(searchBar: UISearchBar) {
      print("searchBarCancelButtonClicked!")
      hideSearchBar()
  }
  
  func searchBarSearchButtonClicked(searchBar: UISearchBar) {
  
      hideSearchBar()
  
       // Do something..
  }
  
  func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
      print("searchBarTextDidBeginEditing!")
  }  
  
  func setSearchBarTitle(streetAddress: NSString){
      //Adding Title Label
      let navigationTitlelabel = UILabel(frame: CGRectMake(0, 0, 200, 21))
      navigationTitlelabel.center = CGPointMake(160, 284)
      navigationTitlelabel.textAlignment = NSTextAlignment.Center
      navigationTitlelabel.textColor  = UIColor.whiteColor()
      navigationTitlelabel.text = "streetAddress" as! String
      self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel
  }
  
  func setSearchBar(){
  
      // Remove the drop shadow from the navigation bar for distinct status bar
      navigationController!.navigationBar.clipsToBounds = true
  
      navigationController!.navigationBar.barTintColor = UIColor.candyGreen()
  }
  
  
  //MARK: Other logic
  </code></pre>
  
  <p>}</p>
  
  <p>My Scene looks like this -
  <a href="https://i.stack.imgur.com/qTaon.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qTaon.png" alt="enter image description here"></a></p>
  
  <p>App crashes on device when search bar edit text field is tapped. Though, it works fine if I comment self.searchBar.becomeFirstResponder() in showSearcBar() resulting in no key for input. Same code works fine on simulator as I can use my development machine keys to enter search bar field. </p>
  
  <p>I'm new to swift development with no previous knowledge of Obj C.</p>
  
  <ul>
  <li>All UI Controls are vanishing from screen leaving just google map as soon as I tapp search bar edit field*</li>
  </ul>
  
  <p><strong>EDIT</strong></p>
  
  <p>Crash Log :</p>
  
  <pre><code>Jan 10 23:33:33 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43328.265701: Stationary,1,Orientation,2,Proximity,0,State,1
  Jan 10 23:33:35 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43329.719682: Stationary,2,Orientation,2,Proximity,0,State,2
  Jan 10 23:33:39 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43333.481776: Stationary,1,Orientation,2,Proximity,0,State,1
  Jan 10 23:33:39 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43333.757166: Stationary,1,Orientation,1,Proximity,0,State,3
  Jan 10 23:33:41 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Warning&gt;: LICreateIconForImage passed NULL CGImageRef image
  Jan 10 23:33:41 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43336.076814: Stationary,1,Orientation,2,Proximity,0,State,1
  Jan 10 23:33:42 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Warning&gt;: HW kbd: Failed to set (null) as keyboard focus
  Jan 10 23:33:42 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Notice&gt;: CoreLocation: CLPocketStateService 43336.927797: Stationary,2,Orientation,2,Proximity,0,State,2
  Jan 10 23:33:50 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Warning&gt;: [MPUSystemMediaControls] Disabling lock screen media controls updates for screen turning off.
  Jan 10 23:33:50 Atul-Kaushiks-iPhone backboardd[56] &lt;Notice&gt;: [HID] [MT] MTSimpleHIDManager::setPropertyInternal detection mode: 3-&gt;255 
  Jan 10 23:33:50 Atul-Kaushiks-iPhone kernel[0] &lt;Notice&gt;: AppleKeyStore: operation failed (pid: 54 sel: 26 ret: e00002e2 '-536870174')
  Jan 10 23:33:50 Atul-Kaushiks-iPhone UserEventAgent[23] &lt;Error&gt;:  LockStateNotifier aksNotificationCallback posting notification: com.apple.mobile.keybagd.lock_status
  Jan 10 23:33:50 Atul-Kaushiks-iPhone UserEventAgent[23] &lt;Notice&gt;: (Note ) PIH: Lock status changed.
  Jan 10 23:33:51 Atul-Kaushiks-iPhone MobileMail[165] &lt;Warning&gt;: Key bag transitioning from unlocked to locking
  Jan 10 23:33:51 Atul-Kaushiks-iPhone UserEventAgent[23] &lt;Warning&gt;: [autosu error]: SPI for AutoSU: unable to find bestSuStart or bestSuEnd with corresponding prob above desired threshold
  Jan 10 23:33:51 Atul-Kaushiks-iPhone com.apple.CDScheduler[23] &lt;Error&gt;: AutoSu doesn't have any prediction yet
  Jan 10 23:33:51 Atul-Kaushiks-iPhone com.apple.CDScheduler[23] &lt;Error&gt;: Failed to get device restart forecast
  Jan 10 23:33:51 Atul-Kaushiks-iPhone SpringBoard[54] &lt;Warning&gt;: [MPUSystemMediaControls] Updating supported commands for now playing application.
  Jan 10 23:33:51 Atul-Kaushiks-iPhone wirelessproxd[55] &lt;Notice&gt;: (Error) updateScanner - central is not powered on: 4
  Jan 10 23:34:00 Atul-Kaushiks-iPhone kernel[0] &lt;Notice&gt;: AppleKeyStore:Sending lock change 1 for handle 0
  Jan 10 23:34:00 Atul-Kaushiks-iPhone UserEventAgent[23] &lt;Error&gt;:  LockStateNotifier aksNotificationCallback posting notification: com.apple.mobile.keybagd.lock_status
  Jan 10 23:34:00 Atul-Kaushiks-iPhone UserEventAgent[23] &lt;Notice&gt;: (Note ) PIH: Lock status changed.
  Jan 10 23:34:00 Atul-Kaushiks-iPhone MobileMail[165] &lt;Warning&gt;: Key bag transitioning from locking to locked
  Jan 10 23:34:13 Atul-Kaushiks-iPhone syncdefaultsd[815] &lt;Notice&gt;: (Note ) marked "com.me.keyvalueservice" topic as "enabled" on &lt;APSConnection: 0x12ee0b070&gt;
  </code></pre>