Dimitre Bogdanov
Dimitre Bogdanov

Reputation: 396

UIScrollView stops scrolling on keyboard dismiss

I'm currently working on an app for iPhone using Swift 3 however I have ran into an issue with the scrollview. Prior to selecting a text field and having the keyboard appear, the scrollview work normally (ie.: I can scroll up and down), however after dismissing the keyboard, the scrollview does not allow me to scroll anymore which is my problem. Note: if I select a text field again and make the keyboard appear it works fine and stops working once it is dismissed again.

I have checked the isScrollEnabled property of the scrollview and it appears to be enabled. Unfortunately I am still not too familiar with all the details of the scrollview and cannot seem to figure out why it has stopped working.

Any help or pointers as to where I could look would be greatly appreciated.

Edit: there is quite a bit of code but here is the narrowed down portion related to scroll view and keyboard:

class ScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
//Scroll view
    @IBOutlet weak var scrollView: UIScrollView!
    //UIView inside the scroll view
    @IBOutlet weak var contentView: UIView!
    //Save button on the top right corner
    @IBOutlet weak var saveButton: UIBarButtonItem!
    //Text field being editted
    var activeTextField:UITextField?

    fileprivate var contentInset:CGFloat?
    fileprivate var indicatorInset:CGFloat?

override func viewDidLoad() {
        contentInset = scrollView.contentInset.bottom
        indicatorInset = scrollView.scrollIndicatorInsets.bottom

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(ScrollViewController.keyboardWillShow(_:)),
                                               name: NSNotification.Name.UIKeyboardWillShow,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(ScrollViewController(_:)),
                                               name: NSNotification.Name.UIKeyboardWillHide,
                                               object: nil)
}

func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
        let userInfo = notification.userInfo ?? [:]
        let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let adjustmentHeight = (keyboardFrame.height + 20) * (show ? 1 : -1)

        scrollView.contentInset.bottom = (contentInset! + adjustmentHeight)
        scrollView.scrollIndicatorInsets.bottom = (indicatorInset! + adjustmentHeight)
    }

    func keyboardWillShow(_ notification: Notification) {
        adjustInsetForKeyboardShow(true, notification: notification)
    }

    func keyboardWillHide(_ notification: Notification) {
        adjustInsetForKeyboardShow(false, notification: notification)
    }

    //Tap gesture to dismiss the keyboard
    @IBAction func hideKeyboard(_ sender: AnyObject) {
        self.view.endEditing(false)
    }

       deinit {
        NotificationCenter.default.removeObserver(self);
    }
}

Upvotes: 0

Views: 1374

Answers (3)

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

Create Extension of UIView and write down this method.

extension UIView {
    func findFirstResponder() -> UIView? {
        if self.isFirstResponder {
            return self
        }
        for subview: UIView in self.subviews {
            let firstResponder = subview.findFirstResponder()
            if nil != firstResponder {
                return firstResponder
            }
        }
        return nil
    }   
}

Upvotes: 2

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

I have create extension of UIViewController and create method for scrollView. Just need to call from viewWillAppear() and viewDidDisappear()

extension UIViewController  {

    func registerForKeyboradDidShowWithBlock (scrollview:UIScrollView ,block: ((CGSize?)  -> Void)? = nil ){
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: nil) { (notification) in
            if let userInfo = (notification as NSNotification).userInfo {
                if let keyboarRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                    if self.view.findFirstResponder() != nil {
                        let  keyboarRectNew = self.view .convert(keyboarRect, to: self.view)
                        let scrollViewSpace = scrollview.frame.origin.y + scrollview.contentOffset.y
                        let textFieldRect:CGRect =  self.view.findFirstResponder()!.convert(self.view.findFirstResponder()!.bounds, to: self.view)
                        let textFieldSpace = textFieldRect.origin.y + textFieldRect.size.height
                        let remainingSpace =  self.view.frame.size.height - keyboarRectNew.size.height

                        if  scrollViewSpace + textFieldSpace > remainingSpace {
                            let gap =  scrollViewSpace + textFieldSpace - remainingSpace
                            scrollview .setContentOffset(CGPoint(x: scrollview.contentOffset.x, y: gap), animated: true)
                        }
                    }
                }
            }
            block?(CGSize.zero)

        }
    }

    func registerForKeyboardWillHideNotificationWithBlock ( scrollview:UIScrollView ,block: ((Void) -> Void)? = nil) {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil, using: { (notification) -> Void in
            scrollview.scrollRectToVisible(CGRect(x: 0, y: 0, width: 0, height: 0), animated: true)
            scrollview.contentOffset = CGPoint(x: 0, y: 0)
            scrollview.contentInset =  UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
            scrollview.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);

            block?()
        })
    }

    func deregisterKeyboardShowAndHideNotification (){

        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)

        self.view.findFirstResponder()?.resignFirstResponder()
    }
}

I have created extension of UIView to create find first responder method in that.

extension UIView {
    func findFirstResponder() -> UIView? {
        if self.isFirstResponder {
            return self
        }
        for subview: UIView in self.subviews {
            let firstResponder = subview.findFirstResponder()
            if nil != firstResponder {
                return firstResponder
            }
        }
        return nil
    }   
}

Upvotes: 2

Davinder Singh
Davinder Singh

Reputation: 645

Their is a third party library in Objective C to handle the keyboard with scroll view, collection view and table view. The name of library is tpkeyboardavoidingscrollview. Try to embed this if possible.

Upvotes: -2

Related Questions