Search code examples
iosswiftuiscrollviewswift4

Scrollview doesn't move up when keyboard appears several times


I've made 30 textfields andvar scrollview: UIScrollView in viewDidLoad() and am trying to move up the whole scrollview up when keyboard covers textfield. Delegate of textfields and scrollview is already set. This code(down below) works and scrollview moves up when keyboard appears for the first time, but the problem is, when keyboard is opened for the second time or more, scrollview doesn't move up.I`ve been unable to solve this problem for several hours. Could anyone detect some faults in my code and give me correct answer? Im looking forward to any help!

The code I've written is this:

var activeField: UITextField?

func registerForKeyboardNotifications(){
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notificaiton:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func deregisterForKeyboardNotifications(){
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWasShown(notificaiton: NSNotification){
    var info = notificaiton.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
    var aRect: CGRect = view.frame
    aRect.size.height -= keyboardSize!.height

    if let activeField = activeField{
        if !aRect.contains(activeField.frame.origin) == true{
            scrollview.contentInset = contentInsets
        }
    }
}

@objc func keyboardWillBeHidden(notification: NSNotification){
    view.endEditing(true)
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    activeField = textField
    registerForKeyboardNotifications()
    return true
}

func textFieldDidEndEditing(_ textField: UITextField){
    activeField = nil
    deregisterForKeyboardNotifications()
}

Solution

  • friend please try this piece of code. Hope it will work for you too. I am using this and it works well.

     override func viewDidLoad(){
        NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardDidShow(notification:)),
        name: UIResponder.keyboardDidShowNotification, object: nil)
        NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardDidHide(notification:)),
        name: UIResponder.keyboardDidHideNotification, object: nil)
    }
    
    //MARK: Methods to manage keybaord
    @objc func keyboardDidShow(notification: NSNotification) {
        var info = notification.userInfo
        let keyBoardSize = info![UIKeyboardFrameEndUserInfoKey] as! CGRect
        scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
        scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
    }
    
    @objc func keyboardDidHide(notification: NSNotification) {
        
        scrollView.contentInset = UIEdgeInsets.zero
        scrollView.scrollIndicatorInsets = UIEdgeInsets.zero
    }
    

    I have written this code for swift 4.0.