Drag and Drop Scroll View that can handle textfields and dismiss keyboard
- Import KeyboardScrollView.swift into your project
- Add ScrollView to storyboard and set the class to KeyboardScrollView
- Set ScrollView constraints:
Leading
,Trailing
,Top
,Bottom
to0
. Set Center X and Ceter Y. - Add reference outlet to your ViewController (named
scrollView
in this example) - Add
UITextFieldDelegate
to your ViewController - Set delegate for each text field that you are using
- Add the following code to your ViewController which provides the scrollView with the ability to adjust focus.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == emailTextField {
textField.resignFirstResponder()
self.scrollView.setContentOffset(CGPoint.zero, animated: true)
return false
}
return true
}