0

I ran the following code in iOS8 using both Apple's Default Keyboards and 3rd Party Keyboards:

class ViewController: UIViewController {

var checkValue: CGRect?

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateViews:", name: UIKeyboardWillShowNotification, object: nil)
}

func updateViews(notification: NSNotification){
    if let userInfo = notification.userInfo {
        let endKeyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey]!).CGRectValue()
        checkValue = endKeyboardFrame
    }
    println(checkValue)
}

}

For example, this is the output for SwiftKeys:

(0.0, 667.0, 375.0, 0.0) 
(0.0, 451.0, 375.0, 216.0)
(0.0, 409.0, 375.0, 258.0)

whereas Apple's Default Keyboard:

Optional((0.0, 409.0, 375.0, 258.0))

It seems for Apple's default keyboards, the code gets called once as expected [e.g. tapping on a textfield]. However for 3rd party keyboards, it gets called multiple times. Why is this? I do not want to disable 3rd party keyboards.

In my main project, I am updating views, which is causing code to run multiple times.

1 Answers1

0

I experienced the same issue. Do some tricks to avoid multiple view updates as follows.
1) Ignore the 1st one by checking if any size is 0 which is invalid.
2) When received any normal size(maybe 2nd one), do not update the view right away, but check if there is one more notification(maybe 3rd one) in the next run loop, and use it.

NeonBerry
  • 372
  • 2
  • 6