您的位置:首页 > 运维架构

(转)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
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 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:
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.


For non-UITableViewControllers, use it as-is by dropping the
TPKeyboardAvoidingScrollView
source files into your project, popping a
UIScrollView
into 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
TPKeyboardAvoidingTableView
source files in, and just make your UITableView a
TPKeyboardAvoidingTableView
in 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐