AziCode
AziCode

Reputation: 2692

Is it possible that the keyboard opens up automatically as soon as the view appears?

I have two view controllers,the 1st IC has a button. when I press on the button the 2nd IC get push on the view. The 2nd IC has a search bar.

Question:

Is it possible that the keyboard opens up automatically as soon as the 2nd interface controller gets pushed into the view

Here's a link to my project: https://www.dropbox.com/sh/5jqwv9vfrtlbh9f/AACOAWy-OAqFTJGgDCYsy-xra?dl=0

enter image description here

enter image description here

Upvotes: 0

Views: 140

Answers (1)

Joe
Joe

Reputation: 61

This should work for the view controller that contains the searchBar:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.searchController.searchBar.becomeFirstResponder()
}

Edited to be in Swift

Edit 2: In looking at your project, you can determine that the searchBar is not ready to be first responder by calling searchBar.canBecomeFirstResponder() in the viewDidAppear() function. I have not dug deep enough to know exactly when the searchBar is able to be first responder, but you can work around this using a NSTimer:

func showKeyboard() {
    self.searchController.searchBar.becomeFirstResponder()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("showKeyboard"), userInfo: nil, repeats: false)
}

Upvotes: 3

Related Questions