Skip to content

Instantly share code, notes, and snippets.

@efremidze
Last active April 28, 2017 04:00
Show Gist options
  • Save efremidze/28dfcf29d2b9ac2d165f60f29117192e to your computer and use it in GitHub Desktop.
Save efremidze/28dfcf29d2b9ac2d165f60f29117192e to your computer and use it in GitHub Desktop.
import Typist
protocol KeyboardAware {
var keyboard: Typist { get }
var contentInset: UIEdgeInsets { get }
var scrollIndicatorInsets: UIEdgeInsets { get }
}
extension KeyboardAware where Self: UIScrollView {
func observeKeyboard() {
keyboard.on(event: .willShow) { [unowned self] options in
self.contentInset.bottom = options.startFrame.height
self.scrollIndicatorInsets = self.contentInset
}.on(event: .willHide) { [unowned self] options in
UIView.animate(withDuration: options.animationDuration) {
self.contentInset.bottom = 0
self.scrollIndicatorInsets = self.contentInset
}
}.start()
}
}
class ScrollView: UIScrollView, KeyboardAware {
let keyboard = Typist.shared
override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
observeKeyboard()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment