Skip to content

Instantly share code, notes, and snippets.

@prat14k
Created February 1, 2018 13:01
Show Gist options
  • Save prat14k/da9e911ef136377941786b2c14e8e68f to your computer and use it in GitHub Desktop.
Save prat14k/da9e911ef136377941786b2c14e8e68f to your computer and use it in GitHub Desktop.
bring textfield above keyboard in a scrollview
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let textfieldPos = scrollView.convert(textField.frame.origin, to: view).y
let textFieldHght = textField.frame.size.height
let scrollViewHght = scrollView.frame.size.height
if (textfieldPos + textFieldHght) > (scrollViewHght - 250) {
var duration : Double
if isKeyBoardVisible {
duration = 0.2
}
else {
duration = 0.5
}
UIView.animate(withDuration: duration, animations: {
self.scrollView.contentOffset = CGPoint(x: 0, y: (textfieldPos + textFieldHght) - (scrollViewHght - 250) + 40)
})
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment