BottomKeyboardConstraint
示例
要运行示例项目,请克隆仓库,并先从 Example 目录运行 pod install
安装
BottomKeyboardConstraint 可以通过 CocoaPods 来使用。为了安装它,只需将以下行添加到您的 Podfile 中
pod 'BottomKeyboardConstraint'
描述
BottomKeyboardConstraint 是一个库,它使处理键盘事件、动画和消失变得简单和透明。
总是建议将配合键盘的视图控制器与 UIScrollView 配对。此库使用您设置的底部键盘来维持从底部到所选 UI 元素的距离。
只需将 bottomKeyboardConstraint 设置为连接底部布局指南/顶部父视图底部和定义的最底部的元素,库将完成其余工作。
整个库都作为 NSLayoutConstraint 的扩展来设置,通过简单的方法 registerAsBottomKeyboardConstraint(in: <KeyboardDelegate 对象>)
激活。
KeyboardDelegate 定义如下:
public protocol KeyboardDelegate {
var bottomKeyboardConstraint: NSLayoutConstraint? { get set }
var view: UIView! { get set }
// Optional methods
func keyboardUpdated(withState state: KeyboardState)
func keyboardShouldDismissAtTap() -> Bool // Default implementation returns true
}
可选地,您可以定义 keyboardUpdated(withState state: KeyboardState)
方法并监听以下键盘事件
public enum KeyboardState {
case willUpdateHeight(from: CGFloat, to: CGFloat)
case didShow
case didSwitch
case willHide
case didHide
}
用法
//.. Import the library
import BottomKeyboardConstraint
//.. Make UIViewController or any other object a delegate for the keyboard
class ViewController: UIViewController, KeyboardDelegate {
//.. Declare a bottomKeyboardConstraint variable either as a @IBOutlet or create it in code and return it
@IBOutlet weak var bottomKeyboardConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
//.. Setup the bottomKeyboardConstraint as the keyboard constraint to have it update as the keyboard shows/updates/hides.
bottomKeyboardConstraint?.registerAsBottomKeyboardConstraint(in: self)
}
//.. Optionally you can also subscribe to the keyboard events
func keyboardUpdated(withState state: KeyboardState) {
print(state)
}
//.. Optionally you can disable the tap to dismiss functionality when the keyboard is visible
func keyboardShouldDismissAtTap() -> Bool {
return false
}
作者
Stefan Brighiu, [email protected]
许可证
BottomKeyboardConstraint 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。