user2497644
user2497644

Reputation: 23

Swift: How to make view controller scroll to text field when keyboard appears

I have a view controller with 8 text fields placed vertically in the view. The problem is that the keyboard obstructs some of the text fields so that the user cannot enter any data. So, basically I need a way to scroll the view.

I did try following the approach listed in this StackOverflow question but had trouble converting some of the objective-c to Swift.

How to make the view controller scroll to text field when keyboard appears

I was wondering if anyone has solved this problem in Swift and if so could post an example?

Thanks in advance!

Upvotes: 1

Views: 2390

Answers (1)

Honghao Z
Honghao Z

Reputation: 1537

Hi I've resolved this problem through subclassing UIScrollView. This is a swift version of scrollView and it works with auto layout.

You can checkout here https://github.com/honghaoz/AutoKeyboardScrollView

Basically, the idea is registering keyboard notifications (e.g. UIKeyboardWillChangeFrameNotification), and scroll to let active textFields visible when keyboarding is showing.

Upvotes: 3

Related Questions