Skip to content
This repository was archived by the owner on May 23, 2022. It is now read-only.

Repository files navigation

KeyboardObserver

For less complicated keyboard event handling.

Carthage compatible

Features

  • Less complicated keyboard event handling.
  • Do not use Notification , but event .

Difference

Without KeyboardObserver.swift

letkeyboardNotifications:[Notification.Name]=[.UIKeyboardWillShow,.UIKeyboardWillHide,.UIKeyboardWillChangeFrame,]overridefunc viewDidLoad(){
super.viewDidLoad()}overridefunc viewWillAppear(animated:Bool){
super.viewWillAppear(animated)
keyboardNotifications.forEach{NotificationCenter.default.addObserver(self, selector: #selector(keyboardEventNotified:), name: $0, object:nil)}}overridefunc viewWillDisappear(animated:Bool){
super.viewWillDisappear(animated)
keyboardNotifications.forEach{NotificationCenter.default.removeObserver(self, name: $0, object:nil)}}@objcfunc keyboardEventNotified(notification:NSNotification){guardlet userInfo = notification.userInfo else{return}letkeyboardFrameEnd=(userInfo[UIResponder.keyboardFrameEndUserInfoKey]as!NSValue).cgRectValue
letcurve=UIView.AnimationCurve(rawValue:(userInfo[UIResponder.keyboardAnimationCurveUserInfoKey]as!NSNumber).intValue)!
letoptions=UIView.AnimationOptions(rawValue:UInt(curve.rawValue << 16))letduration=TimeInterval(truncating:userInfo[UIResponder.keyboardAnimationDurationUserInfoKey]as!NSNumber)letbottom= keyboardFrameEnd.height - bottomLayoutGuide.length
UIView.animate(withDuration: duration, delay:0.0, options:[options], animations:{()->Voidinself.textView.contentInset.bottom = bottom
self.textView.scrollIndicatorInsets.bottom = bottom
}, completion:nil)}

With KeyboardObserver

letkeyboard=KeyboardObserver()overridefunc viewDidLoad(){
super.viewDidLoad()
keyboard.observe{[weak self](event)->Voidinguardlet self =selfelse{return}switch event.type {case.willShow,.willHide,.willChangeFrame:letkeyboardFrameEnd= event.keyboardFrameEnd
letbottom= keyboardFrameEnd.height -self.bottomLayoutGuide.length
UIView.animate(withDuration: event.duration, delay:0.0, options:[event.options], animations:{()->Voidinself.textView.contentInset.bottom = bottom
self.textView.scrollIndicatorInsets.bottom = bottom
}, completion:nil)default:break}}}

How to use

Create KeyboardObserver instance where you want, and the instance observes keyboard untill deinit.

Call observe(event: KeyboardEvent) to observe keyboard events. event is converted keyboard notification object.

publicstructKeyboardEvent{publiclettype:KeyboardEventTypepublicletkeyboardFrameBegin:CGRectpublicletkeyboardFrameEnd:CGRectpublicletcurve:UIViewAnimationCurvepublicletduration:NSTimeIntervalpublicvarisLocal:Bool?publicvaroptions:UIViewAnimationOptions{returnUIViewAnimationOptions(rawValue:UInt(curve.rawValue << 16))}...

event has properties above. You don't have to convert Notification 's userInfo to extract keyboard event values.

KeyboardEentType has types same as keyboard's notification name. Like this below:

publicenumKeyboardEventType{case willShow
case didShow
case willHide
case didHide
case willChangeFrame
case didChangeFrame
...}

It has also public var notificationName: String which you can get original notification name.

Runtime Requirements

  • iOS 8.0 or later
  • Xcode 10.0
  • Swift4.2

Installation and Setup

Information: To use KeyboardObserver with a project targeting lower than iOS 8.0, you must include the KeyboardObserver.swift source file directly in your project.

Installing with Carthage

Just add to your Cartfile:

github "morizotter/KeyboardObserver"

Installing with CocoaPods

CocoaPods is a centralised dependency manager that automates the process of adding libraries to your Cocoa application. You can install it with the following command:

$ gem update
$ gem install cocoapods
$ pods --version

To integrate KeyboardObserver into your Xcode project using CocoaPods, specify it in your Podfile and run pod install.

platform :ios, '8.0'
use_frameworks!
pod "KeyboardObserver", '~> 2.1'

Manual Installation

To install KeyboardObserver without a dependency manager, please add KeyboardObserver.swift to your Xcode Project.

Contribution

Please file issues or submit pull requests for anything you’d like to see! We're waiting! :)

License

KeyboardObserver.swift is released under the MIT license. Go read the LICENSE file for more information.

About

For less complicated keyboard event handling.

Resources

Stars

161 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages