1

I learned from this existing question to get the func "keyboardwillshow" move up the entire view by the same height of the keyboard, in order to avoid the textfields/textviews at the bottom being covered by the keyboard.

However, if I tap two or more textfields in a row, the "keyboardwillshow" func will run by the same amount of tap, moving further up the view and eventually off the screen, leaving only black emptiness.

override func viewDidLoad() {
    super.viewDidLoad()


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}


func keyboardWillShow(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        self.view.frame.origin.y -= keyboardSize.height
    }

}

func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y = 0
}

Someone said in the answers that we can implement a boolean to detect whether the keyboard is already appeared or not, so the func will only work once.

Can somebody show me how to do this boolean? Thanks!

Community
  • 1
  • 1
Juno Ken
  • 43
  • 3

2 Answers2

0

First add an instance variable to store the boolean and initialise to false (var keyboardIsShown = false. Next within the if statement in keyboardWillShow, set the boolean you created earlier to true, as the keyboard has now been shown. Next step is to add another if statement to within keyboardWillShow before you move the view, to check if the keyboard is already showing. Finally make sure to set the boolean back to false when the keyboard is hidden again.

// instance variable here

  override func viewDidLoad() {
        super.viewDidLoad()


        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

    }


    func keyboardWillShow(notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            // if statement here
                self.view.frame.origin.y -= keyboardSize.height
                // set boolean to true
        }

    }

    func keyboardWillHide(notification: NSNotification) {
        self.view.frame.origin.y = 0
        // set boolean to false
    }
Ollie
  • 1,926
  • 1
  • 20
  • 35
0

Ok. I figured out that a better solution is to use UITextFieldTextDidBeginEditingNotification and UITextViewTextDidBeginEditingNotification, or UITextViewTextDidBeginEditingNotification and UITextViewTextDidEndEditingNotification.

Example:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UITextFieldTextDidBeginEditingNotification, object: yourTextField)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UITextFieldTextDidEndEditingNotification, object: yourTextView)

This way, the keyboard behaviour applies to only the specified textfield or textview identified as object:. If you want this to work on all textfields or textviews within your View, then object is nil.

Juno Ken
  • 43
  • 3