A simple approach to handle UITextField & UITextViews over scroll view accross keyboard. The SwiftScrollViews framework is written with the extension UIScrollView.
- Auto scroll for actvie
UITextFied
andUITextView
overUIScrollView
,UITableView
andUICollectionView
. - Set grouping by using
ReturnKeyType
ofUITextField
. - Execute action when last
UITextField
of the group did end editing if need by usingSwiftScrollViewDelegate
. (ReferSwiftScrollViewExample
project). - Set custom
ReturnKeyType
and vertical space between keyboard and activeUITextField
orUITextView
.
- iOS 8.0+
- Xcode 10.1+
pod 'SwiftScrollViews', '~>1.0' # Swift 4.2.1
- Download and drop
Source
folder in your project. - Congratulations!
- Select scrollview in xib or storyboard in viewcontroller.
- Go to Assistand Editor/Identity Inspector/Custom Class/Class as
SwiftScrollView
. If you haveUITableView
then useSwiftTableView
and thenSwiftCollectionView
forUICollectionView
.
It is delegating the method func didEditingDone(for textField: UITextField) {}
will exceute when view did end editing.
The view will end edit when directly taping the ScrollViews or Taping the keyboard return button if textField.returnKeyType != .default || textField.returnKeyType != .next
.
The group of UITextFields
in a view by setting textField.returnKeyType != .default || textField.returnKeyType != .next
at last field of that group.
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)
}
}
I would love you for the contribution to SwiftScrollViews, check the LICENSE file for more info.
Rajamohan S – (https://rajamohan-s.github.io/)
Distributed under the MIT license. See LICENSE for more information.