Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, moving content behind keyboard doesn't reset after dismiss

I have a uiscrollview in one of my VC's. Inside the scrollView,i have multiple TF's, Buttons, etc. Im using the code below, per Apple documentation, to move the scrollView up when a keyboard notification is called, to push up the hidden text fields. However, when I close the keyboard, the scrollView doesn't reset or move back down, it simply stays at the "moved up" location.

Am I missing something here? I have a member variable of the class called:

var activeTextField: UITextField!

Am i using this correctly with the delegate methods?

(func registerForKeyboardNotifications() {
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeShown:",
        name: UIKeyboardWillShowNotification,
        object: nil)
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func keyboardWillBeShown(sender: NSNotification) {
    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    let activeTextFieldRect: CGRect? = activeTextField?.frame
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
        scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
    }
}

// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) {
    let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

func textFieldDidBeginEditing(textField: UITextField!) {
    activeTextField = textField
    scrollView.scrollEnabled = true
}

func textFieldDidEndEditing(textField: UITextField!) {
    activeTextField = nil
    scrollView.scrollEnabled = false
}


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.registerForKeyboardNotifications()
}

override func viewDidDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}
like image 367
candidaMan Avatar asked Sep 15 '14 20:09

candidaMan


2 Answers

I did this and it worked for me. Seems a lot easier then your way. I used a UITextView for my project. But it should work the same with a UITextField. The -/+ 25 was just to allow the title to be shown.

func textViewDidBeginEditing(textView: UITextView) {
    myScrollView.setContentOffset(CGPointMake(0, textView.frame.origin.y-25), animated: true)
}

func textViewDidEndEditing(textView: UITextView) {
    myScrollView.setContentOffset(CGPointMake(0, -textView.frame.origin.y+25), animated: true)
}
like image 183
the_pantless_coder Avatar answered Oct 16 '22 06:10

the_pantless_coder


You have to change the first line of keyboardWillBeHidden

instead of:

let contentInsets: UIEdgeInsets = UIEdgeInsetsZero

write:

let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, 0, 0)

I hope help you

like image 40
Ana Llera Avatar answered Oct 16 '22 06:10

Ana Llera