michaeltyson /TPKeyboardAvoiding
A drop-in universal solution for moving text fields out of the way of the keyboard in iOS
★ 5,773⑂ 909👁 149⑃ 105● 197▤ 0
+0
日增长
5,773
stars
909
forks
149
watchers
105
prs
197
issues
0
releases
README
TPKeyboardAvoiding ================== A drop-in universal solution for moving text fields out of the way of the keyboard in iOS. Introduction ------------ There are a hundred and one proposed solutions out there for how to move UITextField and UITextView out of the way of the keyboard during editing -- usually, it comes down to observing UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, or implementing UITextFieldDelegate delegate methods, and adjusting the frame of the superview, or using UITableView's scrollToRowAtIndexPath:atScrollPosition:animated:, but most proposed solutions tend to be quite DIY, and have to be implemented for each view controller that needs it. This is a relatively universal, drop-in solution: UIScrollView and UITableView subclasses that handle everything. When the keyboard is about to appear, the subclass will find the subview that's about to be edited, and adjust its frame and content offset to make sure that view is visible, with an animation to match the keyboard pop-up. When the keyboard disappears, it restores its prior size. It should work with basically any setup, either a UITableView-based interface, or one consisting of views placed manually. It also automatically hooks up "Next" buttons on the keyboard to switch through the text fields. Usage ----- For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it. For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view. To disable the automatic "Next" button functionality, change the UITextField's return key type to anything but UIReturnKeyDefault. Notes ----- These classes currently adjust the contentInset parameter to avoid content moving beneath the keyboard. This is done, as opposed to adjusting the frame, in order to work around an iOS bug that results in a jerky animation where the view jumps upwards, before settling down. In order to facilitate this workaround, the contentSize is maintained to be at least same size as the view's frame. Licence (Zlib) -------------- Copyright (c) 2013 Michael Tyson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercia
Top issues
#76Scrollview's content too tall after keyboard disappears20 comments · 2013-12-13#49In iOS 7 the ScrollView avoids the status bar14 comments · 2013-09-12#31Keyboard won't resign11 comments · 2013-05-23#92Active scroll before to active a textfield9 comments · 2014-04-08#185UITableView Next responder not working7 comments · 2015-09-17#24On 3.5" iOS device, with a view having Navi controller and 4 textFields, the content size is large then scroll view's frame when keyboard hides.7 comments · 2013-03-22#254UITextField In the first line of the UITableView, it will slide up and slide out of the screen6 comments · 2017-09-21#130does not work with custom keyboard6 comments · 2014-10-04