Search code examples
iosrotationkeyboardresignfirstresponder

How to dismiss keyboard on iPhone 6 after rotation


I am trying to dismiss the keyboard when a uisearchdisplaycontroller is no longer active on the iPhone 6 plus. I can end editing of the search bar but the keyboard stays active on the screen. I have tried many ways, but here is my current code.

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if(searchDisplayController!.active) {
        self.view.endEditing(true)
        self.searchDisplayController!.active = false
    }
}

Any ideas?


Solution

  • Try using below function for detecting rotation:

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    }
    

    Also use resignFirstResponder for hiding the keyboard.