Fast moving content behind the keyboard does not reset after firing

I have a uiscrollview in one of my VC. Inside scrollView, I have some TF buttons, buttons, etc. I am using the Apple documentation code below to move the scroll upward when a keyboard notification is called to push hidden text fields. However, when I close the keyboard, scrollView does not reset or move backward, it just remains in the "moved up" position.

Am I missing something? I have a class member variable:

var activeTextField: UITextField! 

Am I using this correctly using 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 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) } 
+6
source share
4 answers

You must change the first line of keyboardWillBeHidden

instead:

 let contentInsets: UIEdgeInsets = UIEdgeInsetsZero 

records:

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

I hope to help you.

+2
source

I did it and it worked for me. It seems a lot easier than your way. I used UITextView for my project. But it should work with UITextField. - / + 25 is just to show the name.

 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) } 
+2
source

In addition to Anna's answer, you should also use let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0.0, keyboardSize.height, 0.0) in keyboardWillBeShown to avoid the appearance of the text view under the navigation bar.

0
source

The problem is

 var activeTextField: UITextField! 

You should replace activeTextField with another UITextField (for example, a text field existing in the storyboard), and you may find that scrolling can scroll when the keyboard appears.

-1
source

Source: https://habr.com/ru/post/975327/


All Articles