Search code examples
iosswiftuiscrollviewkeyboarduitextfield

How to make scrollView scrolling only if keyboard appears in swift


I am using scrolview for view with height 1000, initially i don't want my scrolView to scroll. if i tap on any textField then i want my scrolview to scroll and if i return keyboard then i don't want my scrollview to scroll.

Here i am able to textfield up when when keyboard appears but i am unable to return textfield to its orginal position when i return keyboard and when i return keyboard i dont want my view to scroll,

Please help me in the code.

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scrolView: UIScrollView!
@IBOutlet weak var upTFLD: UITextField!
var activeTextField = UITextField()

@IBOutlet weak var downTFLD: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    upTFLD.delegate = self
    downTFLD.delegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardAppear(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisappear(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
}
@objc func onKeyboardAppear(_ notification: NSNotification) {

    let info = notification.userInfo!
    let rect: CGRect = info[UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect
    let kbSize = rect.size
    let insets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height+20, right: 0)
    self.scrolView.contentInset = insets
    self.scrolView.scrollIndicatorInsets = insets
    var visibleRect: CGRect = self.scrolView.convert(self.scrolView.bounds, to: self.view)
    visibleRect.size.height -= rect.size.height;
    let inputRect: CGRect = self.activeTextField.convert(self.activeTextField.bounds, to: self.scrolView)
    if (visibleRect.contains(inputRect)) {
        self.scrolView.scrollRectToVisible(inputRect, animated: true)
    }
}

@objc func onKeyboardDisappear(_ notification: NSNotification) {

    self.scrolView.contentInset = UIEdgeInsets.zero
    self.scrolView.scrollIndicatorInsets = UIEdgeInsets.zero
}

public func textFieldDidBeginEditing(_ textField: UITextField) {

    activeTextField = textField
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    activeTextField.resignFirstResponder()
    return true
}
}

initially i dont want scrolling, and if i return keyboard i need textfield come to its original position and no scrolling again.

Only keyboard appears then only i need scrolling. please help me in the code.


Solution

  • You just need to set the contentOffset of your ScrollView right after the keyboard is hidden.

    Create a variable to store offsetBeforeShowKeyboard

    var offsetBeforeShowKeyboard: CGFloat?
    

    When view is initially loaded:

    self.scrollView.isScrollEnabled = false
    

    When select any TextField:

    public func textFieldDidBeginEditing(_ textField: UITextField) {
        self.scrollView.isScrollEnabled = true
        if (self.offsetBeforeShowKeyboard == nil) {
            self.offsetBeforeShowKeyboard = self.scrollView.contentOffset
        }
     }
    

    When keyboard is hidden

     @objc func onKeyboardDisappear(_ notification: NSNotification) {
        self.scrollView.isScrollEnabled = false
        if let offset = self.offsetBeforeShowKeyboard {
            self.scrolView.setContentOffset(offset, animated: true)    
        }
        self.offsetBeforeShowKeyboard = nil
     }