处理UITextField与UITextView跨键盘滚动至scroll view的简单方法。SwiftScrollViews框架是通过将UIScrollView添加扩展编写而成的。
特性
- 对于overscroll至UIScrollView、UITableView和UICollectionView中的活动
UITextFied
和UITextView
自动滚动。 - 使用
UITextField
的ReturnKeyType
设置分组。 - 使用
SwiftScrollViewDelegate
在必要时,当分组中的最后UITextField
完成编辑时执行动作。(参考SwiftScrollViewExample
项目)。 - 设置自定义的
ReturnKeyType
和键盘与活动UITextField
或UITextView
之间的垂直空间。
要求
- iOS 8.0+
- Xcode 10.1+
安装
使用CocoaPods
pod 'SwiftScrollViews', '~>1.0' # Swift 4.2.1
手动安装
- 将项目中的
Source
文件夹下载并放置。 - 恭喜!
使用方法
基本设置
- 在 xib 或 storyboard 的 ViewController 中选择 scrollview。
- 前往 辅助编辑器/身份检查器/自定义类/类 并选择
SwiftScrollView
。如果您有UITableView
,则使用SwiftTableView
,然后对于UICollectionView
使用SwiftCollectionView
。
SwiftScrollViewDelegate
该方法 func didEditingDone(for textField: UITextField) {}
在 视图结束编辑 时执行。
当直接点击 ScrollViews 或点击键盘的返回按钮,如果 textField.returnKeyType != .default || textField.returnKeyType != .next
的时候,视图会结束编辑。
分组
通过在分组中最后一个字段的 textField.returnKeyType != .default || textField.returnKeyType != .next
来设置 一个视图中的 UITextFields
分组。
附加配置
SwiftScrollViews.config.textComponentSpaceFromKeyboard = 40 //vertical space between keyboard and active text field or text view.
SwiftScrollViews.config.defaultDoneKey = .done // The default return key of last text field over the scroll view.
示例
class ScrollViewExample: UIViewController,SwiftScrollViewDelegate {
@IBOutlet weak var scrollView: SwiftScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.swiftScrollViewsDelegate = self
}
func didEditingDone(for textField: UITextField) {
//TODO:- Do further with by comparing textField with self.textField
let controller = UIAlertController(title: textField.placeholder ?? "Place Holder Nil", message: "✅ Editing Done!", preferredStyle: .alert)
controller.addAction(UIAlertAction(title: "👍", style: .default, handler: nil))
self.present(controller, animated: true, completion: nil)
}
}
贡献
我非常欢迎对 SwiftScrollViews 的贡献,有关更多信息,请查看 LICENSE 文件。
元信息
Rajamohan S – (https://rajamohan-s.github.io/)
在 MIT 许可证下分发。请查阅 LICENSE 文件以获取更多信息。