18

I've created a custom UIViewController with one UITextField on Storyboard. On viewDidLoad, I set the UITextFIeld to becomeFirstResponder, nothing happened (no keyboards popped up).

I then tried calling resignFirstResponder(), but it returned false. Next I tried to find who the first responder is through looping all the subviews using the code over @ Get the current first responder without using a private API. It turns out none of the sub views are the first responder.

My ViewController in storyboard

enter image description here

My Code

class BLTestViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var tf: UITextField

    override func viewDidLoad() {
        super.viewDidLoad()

        tf.delegate = self
        tf.becomeFirstResponder()
        // Do any additional setup after loading the view.
    }
}

As simple as it gets...WHY ISN'T THE KEYBOARD COMING UP!!! One thing I do have on is auto layout, but I'm not sure if that affects the keyboard popping up.

Community
  • 1
  • 1
blee908
  • 10,735
  • 9
  • 31
  • 39
  • Try making your text field the first responder in `viewWill/DidAppear:`. – duci9y Aug 17 '14 at 21:18
  • Tried both, no luck. – blee908 Aug 17 '14 at 21:24
  • Is the text field connected to your view controller with an outlet connection? – duci9y Aug 17 '14 at 21:25
  • 1
    quick idea, I think you may still be calling it too early in viewDidAppear etc... just to test can you set up button that calls a method containing tf.becomeFirstResponder() --- this way we can rule out timing as a problem – Woodstock Aug 17 '14 at 21:27
  • Yes the textField is connected as an IBOutlet. I tried putting `tf.becomeFirstResponder()` in `func textFieldShouldBeginEditing(textField: UITextField!) -> Bool` which is where I ultimately want to be and is later than `viewDidAppear`, but got a "BAD Access Exception. – blee908 Aug 17 '14 at 21:30
  • Are you testing in the simulator? Keyboard doesn't appear in simulator in beta 4 for me. Running on device shows it though. – Eugene Aug 17 '14 at 21:41
  • Yes I'm running on the simulator, I'm trying to run it on the device, but it saids "No provisioned iOS devices are available with a compatible iOS version..." i just updated to the latest iOS version with my iPhone 5. – blee908 Aug 17 '14 at 22:39
  • The problem only appears on Xcode 6 b5 with the iPhone 4s simulator. This code works well for the iPhone 5s simulator. You should file a radar. – Imanou Petit Aug 17 '14 at 22:39
  • Side note: you shouldn't call `becomeFirstResponder()` from within `textFieldShouldBeginEditing()` – Aaron Brager Aug 17 '14 at 22:48
  • 1
    @Eugene. I think you're right, this seems to be an issue with the xCode 6 simulator itself!!! God dammit Apple. – blee908 Aug 18 '14 at 00:23
  • @bennett It appears that there's quite an easy way to bring the keyboard up anyway. Select the text field you'd like to edit in the simulator and press command+k – Eugene Aug 18 '14 at 16:41
  • 2
    See [this question][1]. Basically press Apple+K [1]: http://stackoverflow.com/questions/24420873/swift-xcode-6-keyboard-not-showing-up-in-ios-simulator – nflacco Aug 19 '14 at 05:09
  • Sorry it didn't work for you, but your code worked great for me. +1 – stevenspiel Oct 01 '14 at 00:21
  • look at this [solution][1], it'll help ;) [1]: http://stackoverflow.com/questions/24420873/swift-xcode-6-keyboard-not-showing-up-in-ios-simulator – dimpiax Oct 09 '14 at 12:52
  • try before canFirstResponder before, becomeFirstResponder. – Vineesh TP Dec 11 '14 at 07:31

6 Answers6

24

Swift 4 it worked for me
try it

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    textView.becomeFirstResponder()
    
}
Kiril S.
  • 8,572
  • 5
  • 34
  • 56
  • Executing from inside dispatch queue did it for me. – rodrigochousal Jan 15 '19 at 19:05
  • 2
    `viewDidAppear` is ran on the main thread, ie **delaying** is what's needed, not dispatching to **main**. If you're already on main thread and then dispatch to main, then whatever that you've just dispatched will go to the end of the main queue. I suppose: `DispatchQueue.global(qos: .userInteractive).async{}` would also fix the issue, due to the delay it creates. A view must have a Window before it becomes the responder, and that can only happen **after** viewcontroller's `viewWillAppear` or `viewDidAppear` is called. So any attempt to make a view the responder before is overridden – Honey Oct 25 '19 at 21:26
20

I just tested it with a textfield by calling self.tf.becomeFirstResponder() indside viewDidLoad() function and its working absolutely fine. You'll need to toggle your keyboard by pressing command + K as nflacco just pointed out and disable the hardware keyboard in the simulator as:

  1. iOS Simulator -> Hardware -> Keyboard
  2. Uncheck "Connect Hardware Keyboard"
4aRk Kn1gh7
  • 3,803
  • 1
  • 27
  • 41
6

I think

class BLTestViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var tf: UITextField

    override func viewDidLoad() {
        super.viewDidLoad()

        tf.delegate = self
        self.focustf()
        // Do any additional setup after loading the view.
    }

    func focustf(){
    self.tf.becomeFirstResponder()
    }
}
Hitesh
  • 3,092
  • 8
  • 33
  • 50
Hoàng Kiên
  • 61
  • 1
  • 2
4

Swift 4 and iOS 11

In my case, no mater what I tried, I was not able to set first responder until I tried this.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.isKind(of: YOURTABLEVIEWCELL.self) {
        if let yourCell = cell as? YOURTABLEVIEWCELL{
            yourCell.yourUiTextField.becomeFirstResponder()
        }
    }
}

Hope this helps someone stuck with the same problem.

  • in my case i need to focus on custom header section textfield when the user click the row of the section. is it possible? I tried to put the same way in did select row at index path but its not working for me.! – Hari Narayanan Sep 19 '18 at 13:07
  • @HariNarayanan , might try similar to above but with tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) – Derek Hierholzer Nov 07 '18 at 21:13
0

press command+shift+k for open the keyboard.

Talha Ahmed
  • 127
  • 8
0

try with this

 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    textField.becomeFirstResponder()
                }

sometimes the wrong is the time in render the view

x-rw
  • 1,253
  • 10
  • 24