There are a hundred and one proposed solutions out there for how to move UITextField andUITextView 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‘sscrollToRowAtIndexPath: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 andUITableView 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 theTPKeyboardAvoidingScrollView source files into your project, popping aUIScrollView into your view controller’s xib, setting the class toTPKeyboardAvoidingScrollView, and putting all your controls within that scroll view.

To use it with UITableViewController, pop theTPKeyboardAvoidingTableView source files in, and just make your UITableView aTPKeyboardAvoidingTableView 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

 

(转)A drop-in universal solution for moving text fields out of the way of the keyboard的更多相关文章

  1. Educational Codeforces Round 91 (Rated for Div. 2) B. Universal Solution

    题目链接:https://codeforces.com/contest/1380/problem/B 题意 你在和一个机器人玩石头剪刀布,给出一个长为 $n$ 的出拳序列,机器人会从某一处开始出拳 $ ...

  2. Educational Codeforces Round 91 (Rated for Div. 2) B. Universal Solution (贪心)

    题意:石头剪刀布,bot有一串字符,表示他要出什么,你需要事先确定你的出招方案,然后遍历bot的字符串,从\(i\)位置开始跑一个循环,每次跑都要记录你赢的次数贡献给\(sum\),现要求\(\fra ...

  3. github上所有大于800 star OC框架

    https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...

  4. iOS.OpenSource.AllInOne

    Open Source Project for iOS 所有和iOS相关的Open Source Project的汇总. 功能点 开源项目   iOS Gallery RMGallery https: ...

  5. 拖放事件(drop events)在Firefox上运行会出现的问题

    可能会有人觉得我废话特别多,我就在开头写一个简单粗暴的版本: 在Firefox中ondrop事件会触发Firefox自带的拖拽搜索功能,在ondrop事件触发执行时触发的函数中加上这两条: /* 禁止 ...

  6. Item 25: 对右值引用使用std::move,对universal引用则使用std::forward

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 右值引用只能绑定那些有资格被move的对象上去.如 ...

  7. ZetCode PyQt4 tutorial Drag and Drop

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...

  8. Simple drag and drop

    In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) ...

  9. Solution -「JOISC 2019」「LOJ #3036」指定城市

    \(\mathcal{Description}\)   Link.   给定一棵含 \(n\) 个结点的树,双向边权不相同.\(q\) 次询问,每次询问在树上标记 \(e\) 个点,标记的价值为所有趋 ...

随机推荐

  1. KVM硬件辅助虚拟化之 EPT(Extended Page Table)

    传统OS环境中,CPU对内存的訪问都必须通过MMU将虚拟地址VA转换为物理地址PA从而得到真正的Physical Memory Access,即:VA->MMU->PA,见下图. 虚拟执行 ...

  2. Android 字体设置

    Android 对中文字体支持很不好~~ 需要加入相应的字体库 (1)创建布局Layout //创建线性布局 LinearLayout linearLayout=newLinearLayout(thi ...

  3. NYOJ 16 矩形嵌套(动态规划)

    矩形嵌套 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅 ...

  4. Windows server 2008系统下FTP服务器的安装

    一.在 Windows 服务器上安装 FTP 服务 1. 在"开始"菜单上,单击"管理工具",然后单击"服务器管理器". 2. 在" ...

  5. 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)

    源:http://www.cnblogs.com/beginor/archive/2013/01/13/2858228.html 慢的问题. Intel HAXM (Hardware Accelera ...

  6. 动态更新UI的方式

    1. TimerTask 和 timer连用: 这里主要是实现倒计时, TimerTask 里面有方法runOnUiThread,在这个方法里面调用timer cancel()停止倒计时,同样更新UI ...

  7. Zookeeper+Kafka+Storm+HDFS实践

    Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. Hadoop一般用在离线的分析计算中,而storm区别于hadoop,用在实时的流式计算中,被广泛用来 ...

  8. DX笔记之五------游戏画面绘图之绘制位图

    本系列文章由zhmxy555编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7335103 共四步 步骤一:加载位图 步骤二:建立与 ...

  9. arm+linux 裸机环境搭建之初试minicom+dnw烧写uboot

    下面的步骤将会实现在linux下面使用dnw+minicom来烧写uboot 一.安装minicom 下载地址:http://download.csdn.net/detail/king_bingge/ ...

  10. .net转php laraval框架学习系列(二)项目实战---Models

    上一篇已经介绍开发环境的搭建,如果有问题可以在文章后留言. 这篇将从项目实战开发,一步一步了解laravel框架. 在开发mvc项目时,models都是第一步. 下面就从建模开始. 实体关系图 由于不 ...