Keyboard
示例
要运行示例项目,克隆仓库,然后首先在 Example 目录中运行 pod install
。
要求
- Xcode 9.0+
- iOS 8.0+
安装
您可以通过 CocoaPods 获取 Keyboard。安装时只需将以下行添加到 Podfile 中
pod 'KeyboardSwift'
使用
import KeyboardSwift
您可以在包含 inputAccessoryView
的 UIViewController
或任何 UIView
(如 UITextField
,UISearchBar
,UITextView
)中使用 Keyboard。只需调用 keyboard
并根据您的需求配置项目即可。例如:
let cancelItem = KeyboardItem.barButton(title: "Cancel", style: .plain) { item in
// Called when cancel item is tapped
self.textView.endEditing(true)
}
let countLabel = UILabel()
countLabel.text = "0"
let labelItem = KeyboardItem.custom(view: countLabel)
let doneItem = KeyboardItem.barButton(title: "Done", style: .done) { item in
// Called when done item is tapped
self.textView.endEditing(true)
}
textView.keyboard.with(items: cancelItem, .flexibleSpace, labelItem, .flexibleSpace, doneItem)
键盘项
/// Default bar button item
case barButton(title: String, style: UIBarButtonItemStyle, action: UIBarButtonItemTargetClosure?)
/// System bar button item
case systemBarButton(system: UIBarButtonSystemItem, action: UIBarButtonItemTargetClosure?)
/// Flexible space
case flexibleSpace
/// Fixed space
case fixedSpace(width: CGFloat)
/// Custom view
case custom(view: UIView)
自定义
您可以通过调用customize
方法来自定义accessoryView
textView.keyboard.customize { (toolbar, items) in
toolbar.isTranslucent = false
toolbar.tintColor = .purple
}
键盘事件
仅在UIViewController
或其子类中可用
case willShow
case willHide
case didShow
case didHide
示例
override func viewDidLoad() {
super.viewDidLoad()
// Subscribe
self.keyboard.subscribe(to: .willShow) { sender in
// TODO: something to do when keyboard will be shown
}
// ...
// Unsubscribe
self.keyboard.unsubscribe()
}
作者
[email protected],阿德里安·布斯卡·科尔雷亚
许可证
键盘项在MIT许可下可用。有关更多信息,请参阅LICENSE文件。