当键盘显示时,保持任何 UIView 可见的优雅解决方案
要求
IHKeyboardAvoiding 版本 | |
---|---|
Objective-C | 2.6.0 |
Swift | 3.0.0 + |
描述
IHKeyboardAvoiding 当键盘显示时,会将任何 UIView 移动到上方,当键盘隐藏时,它会恢复到原始位置。
将 avoidingView
(UIView) 注册到 IHKeyboardAvoiding;当键盘显示时,如果 avoidingView
的边框与键盘相交,它将上移到键盘上方。当键盘被关闭时,avoidingView
将恢复到其原始位置。
IHKeyboardAvoiding与其他键盘回避解决方案的区别在于,它不需要将内容放在UIScrollView中。没有任何滚动视图被使用。它不仅限于保持UITextFields可见,使用IHKeyboardAvoiding可以避免任何UIView的键盘。
如果使用Autolayout,则约束会动画化,否则将执行CGAffine translation。
支持的功能
- iPhone键盘
- iPad连接的键盘
- iPad断开连接的键盘
- iPad分屏键盘
- 横屏 & 字符画
- 第三方键盘
- 自动布局
- 自适应掩码(弹簧 & 拉索)
如何安装
将其添加到您的 CocoaPods Podfile。
pod 'IHKeyboardAvoiding'
如何使用
设置回避视图
KeyboardAvoiding.avoidingView = self.avoidingView
根据您的使用情况,将其放在viewDidLoad()
或viewDidAppear(_ animated: Bool)
中
如果您不确定,将其放在viewDidAppear(_ animated: Bool)
中
如果您需要动态设置回避视图或其属性,请考虑将其放在textViewShouldBeginEditing(_ textView: UITextView)
中
可选方法
KeyboardAvoiding.setAvoidingView(self.avoidingView, withTriggerView: self.triggerView)
使用此方法设置回避视图,但有不同的视图触发回避。如果触发视图的框架与键盘相交,则回避视图将移动,使触发视图位于键盘之上。KeyboardAvoiding.buffer
当回避视图与触发视图框架的距离在buffer
点内时,回避视图将移动。默认buffer为0。
KeyboardAvoiding.paddingForCurrentAvoidingView
键盘和触发视图之间的填充值。默认填充值为0。
通过将任何UIView更改为KeyboardDismissingView的实例来启用轻点以关闭键盘的功能。
现在购买并免费获得这份礼物:)
Codable扩展,用于解码数组,抓取并记录所有解码失败https://github.com/IdleHandsApps/SafeDecoder
居中且可重用的按钮样式,并连接到InterfaceBuilder https://github.com/IdleHandsApps/DesignableButton
用于无网络连接信息的完全自动状态栏覆盖 https://github.com/IdleHandsApps/StatusBarOverlay
IHKeyboardAvoiding 与 UIScrollView 解决方案 - 战斗,战斗!
UIScrollView 优点
- 快速又简单
IHKeyboardAvoiding 优点
- 你的应用中不会零散地出现多个scrollview
- 在视图层次结构中拥有多个scrollview可能会引发问题
- 当键盘隐藏时,scrollview不一定总是滚动回原始位置
- scrollview 只会滚动到足以保持焦点文本框可见的程度
- IHKeyboardAvoiding 提供控制键盘出现时哪些UIView可见的功能
类似键盘规避解决方案
https://github.com/michaeltyson/TPKeyboardAvoiding (基于UIScrollView)
https://github.com/kirpichenko/EKKeyboardAvoiding (基于UIScrollView)
https://github.com/robbdimitrov/RDVKeyboardAvoiding (基于UIScrollView) https://github.com/hackiftekhar/IQKeyboardManager (看起来很有趣) https://github.com/danielamitay/DAKeyboardControl (看起来很有趣)
作者
- Fraser Scott-Morrison ([email protected])
非常乐意了解任何使用 IHKeyboardAvoiding 的酷应用
许可证
在MIT许可证下分发