3

When I press on a UITextField that is on the lower part of the screen, it is hidden by the keyboard. What I wanted to do is moving up the view, with the standard iOS animation, reaching the UITextField that in which I am inserting some text. I am developing the app in Swift 5 (Xcode 10.2)

The result that I have reached is that now I can move the view (a little earlier than desired) but the view moves every time I press on a UITextField, not only the one that will be hided by the keyboard.

class ViewController: UIViewController {
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

    @objc func keyboardWillShow(notification: NSNotification) {
        guard let userInfo = notification.userInfo else {
            return
        }

        guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }

        let keyboardFrame = keyboardSize.cgRectValue

        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardFrame.height
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        guard let userInfo = notification.userInfo else {
            return
        }

        guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }

        let keyboardFrame = keyboardSize.cgRectValue

        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y += keyboardFrame.height
        }
    }
}

The result that I want to obtain is that if the user presses on a UITextField that will be hided by the keyboard, then, a little bit after the keyboard appeared, the view is moved up until the user can see the UITextField that has pressed.

I've searched a long for a solution to this problem but all others that I've seen seems outdated or not doing what I'm trying to achieve.

Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
vFeder6
  • 51
  • 1
  • 4
  • Try this: https://stackoverflow.com/questions/55561143/when-keyboard-present-choose-what-causes-view-to-slide-up/55561272#55561272 – Razi Tiwana Apr 11 '19 at 19:33
  • Possible duplicate of [How can I make a UITextField move up when the keyboard is present - on starting to edit?](https://stackoverflow.com/questions/1126726/how-can-i-make-a-uitextfield-move-up-when-the-keyboard-is-present-on-starting) – Razi Tiwana Apr 11 '19 at 20:49
  • @RaziTiwana thanks, what you posted in the first comment works, but it adds more features that I honestly don't want. The second comment reports something in Objective-C that I don't understand and refers to an old iOS version. – vFeder6 Apr 11 '19 at 21:20
  • Is the additional tool bar the only problem you have with the first one? cuz that can be removed. – Razi Tiwana Apr 11 '19 at 21:29
  • You have check in func keyboardWillShow(notification: NSNotification) {} that which textfield beginsEditing, if textfield which is bottom then only move view up. – Nikhil Nandha Apr 12 '19 at 07:01

1 Answers1

4

you can try by taking scrollview :

import UIKit

class ViewController: UIViewController {

    @IBOutlet var scrollView: UIScrollView!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        NotificationCenter.default.addObserver(self,selector:#selector(self.keyboardWillShow),name:UIResponder.keyboardDidShowNotification, object: nil)
        NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardWillHide),name:UIResponder.keyboardDidHideNotification, object: nil)

    }

    @objc func keyboardWillShow(notification: Notification) {

        guard let userInfo = notification.userInfo,

            let frame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
            else{
                return

        }

        let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: frame.height, right: 0)

        scrollView.contentInset = contentInset

    }

    @objc func keyboardWillHide(notification: Notification)

    {
        scrollView.contentInset = UIEdgeInsets.zero
    }

}
  • I have all my elements in a stackview. Is it possibile to add the scrollview programmatically? I cannot find a way – vFeder6 Apr 13 '19 at 10:33
  • @vFeder6 i never tried scrollview programmatically. but u can embed ur stack view with scrollview. – Santosh Kumar J M Apr 15 '19 at 06:59
  • I tried with the scroll view and it works. Now I have a different problem. When I pass from one TextField to another, the notification keyboardWillShow is not raised, so I have the same problem but when the keyboard is already shown. Can I update the main question with the solution and ask for another question? – vFeder6 Apr 21 '19 at 13:13