Created
February 1, 2018 13:01
-
-
Save prat14k/da9e911ef136377941786b2c14e8e68f to your computer and use it in GitHub Desktop.
bring textfield above keyboard in a scrollview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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