4

I have a UITextField with custom keyboard, basically like a calculator. I would like my keyboard show up by default, so i used [self.topInputTextField becomeFirstResponder]; in viewDidLoad, I think that's very common usage. However, it causes some very weird actions on my textfield.

When i set my textfield as the first responder in viewDidLoad, and every time after i done editing, the text will jump, and when i click another text field and and click the first text field again, the texts in the first text field sometimes shift down and disappear, but sometimes not. I feel it's very hard to describe, so i recorded a GIF image for it.

enter image description here

And the reason, that I am sure [self.topInputTextField becomeFirstResponder]; causing the issue, is when i comment that line of code out, everything back to normal. here is the GIF after i comment out that line:

enter image description here

that's vert strange to me, between 2 GIF file, the only change i did is comment out that line of code. I couldn't find any solution on SE. Any idea would be very appreciated.

Edit: One more thing is I tried to change font, and font sizes, they all have similar strange behaviors.

*Edit 2:** here is how i set up my textfield,i didn't do anything fancy

enter image description here

Xu Yin
  • 3,932
  • 1
  • 26
  • 46

2 Answers2

5

Try calling the keyboard in viewDidAppear, this method gets called after viewDidLoad. I assume it's because you should only call the keyboard on a loaded view that has appeared to the user, so if you call it before the view actually appears it will cause unexpected behaviour.

David P
  • 617
  • 1
  • 15
  • 26
4

viewDidLoad is too early for calling this, the UI hasn't worked out which size your screen is, or even which orientation your device is in. It isn't yet a UI really... Try it in willAppearAnimated: ..

Jef
  • 4,728
  • 2
  • 25
  • 33
  • 1
    Did you try both `viewWillAppear` and `viewDidAppear`? – David P Feb 20 '14 at 06:51
  • @Chicken actually just tried viewDidAppear, it solved this issue.. viewWillAppear will not.. can you add that as the answer, and would you mind to explain a little bit? – Xu Yin Feb 20 '14 at 06:56
  • Jef's reasoning was correct, the actual drawing of UI wasn't finalised in viewDidLoad. Thanks :) – David P Feb 20 '14 at 07:05
  • 1
    well thats about the cheapest 15 points you'll ever win on stack ya pirate :p bravo – Jef Feb 20 '14 at 07:54
  • 1
    @chicken no hard feelings whatsoever, a joke is all mate – Jef Feb 20 '14 at 08:16