(转)A drop-in universal solution for moving text fields out of the way of the keyboard
2013-08-12 00:37
736 查看
There are a hundred and one proposed solutions out there for how to move
I thought I’d put together a relatively universal, drop-in solution:
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.
For non-UITableViewControllers, use it as-is by dropping the
To use it with UITableViewController, pop the
You can grab the source files, which includes a sample project, over on the GitHub project page
UITextFieldand
UITextViewout of the way of the keyboard during editing — usually, it comes down to observing
UIKeyboardWillShowNotificationand
UIKeyboardWillHideNotification, or implementing
UITextFieldDelegatedelegate methods, and adjusting the frame of the superview, or using
UITableView‘s
scrollToRowAtIndexPath:atScrollPosition:animated:, but all the proposed solutions I’ve found tend to be quite DIY, and have to be implemented for each view controller that needs it.
I thought I’d put together a relatively universal, drop-in solution:
UIScrollViewand
UITableViewsubclasses 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.
For non-UITableViewControllers, use it as-is by dropping the
TPKeyboardAvoidingScrollViewsource files into your project, popping a
UIScrollViewinto your view controller’s xib, setting the class to
TPKeyboardAvoidingScrollView, and putting all your controls within that scroll view.
To use it with UITableViewController, pop the
TPKeyboardAvoidingTableViewsource files in, and just make your UITableView a
TPKeyboardAvoidingTableViewin the xib — everything should be taken care of.
You can grab the source files, which includes a sample project, over on the GitHub project page
相关文章推荐
- Are you looking for a way to get the entire text of a word document into a RichEdit without using the Clipboard?
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- ADF: The best way to indicate for records in a table the existence of details
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
- an out of memory error has occurred.Consult the 'running eclipse' section of the read me file for...
- 【ask】Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
- 2 solution of java.lang.OutOfMemoryError in Java
- MMSEG: A Word Identification System for Mandarin Chinese Text Based on Two Variants of the Maximum M
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- Error (10309): VHDL Interface Declaration error in keyboard.vhd(63): interface object "scan_code" of mode out cannot be read. Change object mode to bu
- Project - Build a Software for Traffic Control in the City of Harbin
- MapReduce报错:Error: java.io.IOException: Initialization of all the collectors failed. Error in last collector was :interface javax.xml.soap.Text
- Linux Annoyances for Geeks : Getting the Most Flexible System in the World Just the Way You Want It
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- Pixel Width of the text in a UILabel