1

I have added observers so that I can move textFields above keyboard. Its working fine on all devices but keyboardWillAppear function is not called when running in simulator iPhone 4S (iOS 9). This is happening when I "Connect Hardware Keyboard"

keyboardWillHide is being called instead of keyboardWillShow

keyboard

override func viewDidLoad() {
  super.viewDidLoad()

  NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
  NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}


func keyboardWillAppear(notification: NSNotification){
  // This is not called on simulator iPhone 4S(iOS 9.0)
}

func keyboardWillDisappear(notification: NSNotification){
  // This is called
}

I really can't understand this behaviour. Can someone please explain the internal functionality which leads to this.

sasquatch
  • 6,126
  • 7
  • 39
  • 56
  • you need to press toggle keyboard option too!!! – Teja Nandamuri Nov 08 '15 at 14:09
  • What does that do? And why does the problem happens only on 4S? – sasquatch Nov 08 '15 at 14:10
  • when a keyboard doesn't popup on the simulator, you need to press toggle keyboard, then keyboard will appear on simulator – Teja Nandamuri Nov 08 '15 at 14:12
  • If you want to be sure about the behaviour of keyboard, I will recommend with actual device. – Yuchen Nov 08 '15 at 14:14
  • 1
    @YuchenZhong Its working fine on actual device. But my app was rejected by App Store. And the only crash I can find is on simulator. – sasquatch Nov 08 '15 at 14:15
  • Hmm, interesting to know. But it sounds reasonable to me that if it is connect to hardware keyboard, the `keyboardWillShow` function won't get called. It is a bit weird to see that `keyboardWillHide` is called though. But no matter what, you probably will be able to fix the crash and you should. Otherwise, it will still crash your app if user connect to external keyboard on their phone. – Yuchen Nov 08 '15 at 14:22
  • Yes, but I am totally unable to understand this behaviour. And its not happening on iPhone 5 or 6 even on simulator. – sasquatch Nov 08 '15 at 14:26
  • I don't know why it would happen on only one simulator, but if there's a crash caused by code that's run in those two methods, then you need to add code that checks for the actual keyboard moving, such as finding the height of the keyboard. – Tim Nov 08 '15 at 14:43
  • @Tim I went through a lot of questions, and using `NSNotificationCenter` is commonly accepted solution. http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – sasquatch Nov 08 '15 at 14:49
  • I understand that, I'm just trying to suggest a workaround that "double-checks" the keyboard. – Tim Nov 08 '15 at 15:22
  • @Tim The keyboard height is also not 0, how do I "double-check" the keyboard? – sasquatch Nov 08 '15 at 20:29
  • In your `keyboardWillAppear`, `let dict = notification.userInfo! kbdFrame = (dict[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() kbdFrame = self.view.convertRect(kbdFrame, fromView:nil)` – Tim Nov 08 '15 at 21:41

0 Answers0