3

I have six UITextFields in my view and I want to decide whether the view has to move or not. How can I check which TextField is selected before moving my view?

Thats my Code for showing the keyboard and moving the view:

    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 - 85
            }
    }

    func keyboardWillHide(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                self.view.frame.origin.y += keyboardSize.height - 85
            }
    }
Philipp Rosengart
  • 713
  • 1
  • 8
  • 20
  • 3
    http://stackoverflow.com/questions/25693130/move-textfield-when-keyboard-appears-swift – Akash Jan 29 '16 at 16:41

1 Answers1

3

If you want to know which UITextField is selected, you can use textFieldDidBeginEditing and textFieldDidEndEditing

func textFieldDidBeginEditing(textField: UITextField!) {
    currentSelectedTextField = textField
}

func textFieldDidEndEditing(textField: UITextField!) {
    currentSelectedTextField = nil
}

For your reference about how to manage keyboard when selecting a UITextField: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

Breek
  • 1,431
  • 13
  • 24