I'm new here and im starting with Swift for iOS.
I started creating a simple app that does some operations. But I'm having some problems when the keyboard appears, hiding one of my textFields. I think it's a common problem and I did some research but I couldn't find anything that solved my problem. And I want to use a Scroll rather than animate the textField to make it visible.
Thanks!!!!! (sorry for english mistakes)
contentInset
doesn't work for me, because I want the scrollview move all the way up above the keyboard. So I usecontentOffset
:Here is a complete solution in Swift 4, utilizing guard and concise code. Plus correct code in
keyboardWillHide
to only reset thebottom
to 0.You can animate your scrollview to center on your UITextField on keyboard appearance (ie. making your textfield the first responder) via a scroll offset. Here are a couple of good resources to get you started (there are a bunch on this site):
How programmatically move a UIScrollView to focus in a control above keyboard?
How to make a UIScrollView auto scroll when a UITextField becomes a first responder
Additionally, if you simply use a UITableView with your content in cells, when the textfield becomes first responder, the UITableViewController will automatically scroll to the textfield cell for you (though I'm not sure this is what you want to do).
for Swift 4.0
In ViewDidLoad
Add below observer methods which does the automatic scrolling when keyboard appears.
Reading the links you sent to me, I found a way to make it work, thanks!:
From the answer by Sudheer Palchuri, converted for Swift 4.
In ViewDidLoad, register the notifications:
And then: