Skip to content

Instantly share code, notes, and snippets.

import Foundation
class KeyboardAppearListener {
private var showKeyboard: NotificationToken?
private var hideKeyboard: NotificationToken?
private weak var viewController: UIViewController?
private var windowSafeArea: CGFloat = 0
private var windowsSafeAreaWereAdded = false
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey]
.flatMap { $0 as? Double } ?? 0.25
if duration > 0 {
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey]
.flatMap { $0 as? Int }
.flatMap { UIView.AnimationCurve(rawValue: $0) } ?? .easeInOut
UIViewPropertyAnimator(duration: duration, curve: curve) {
viewController.view.layoutIfNeeded()
}
.startAnimation()
let windowSafeArea: CGFloat = windowsSafeAreaWereAdded ? 0 : viewController.view.safeAreaInsets.bottom - viewController.additionalSafeAreaInsets.bottom
viewController.additionalSafeAreaInsets.bottom += beginKeyboardBounds.origin.y - endKeyboardBounds.origin.y - windowSafeArea
windowsSafeAreaWereAdded = true
viewController.additionalSafeAreaInsets.bottom += beginKeyboardFrame.origin.y - endKeyboardFrame.origin.y
guard
let viewController = viewController,
let userInfo = notification.userInfo,
let beginKeyboardFrame = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect,
let endKeyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
endKeyboardFrame != beginKeyboardFrame
else {
return
}
class KeyboardAppearListener {
private var showKeyboard: NotificationToken?
private var hideKeyboard: NotificationToken?
private weak var viewController: UIViewController?
init(
_ viewController: UIViewController,
notificationCenter: NotificationCenter = .default) {
self.viewController = viewController
showKeyboard = notificationCenter.observe(
name: UIResponder.keyboardWillShowNotification) { [weak self] (notification) in
class ViewController: UIViewController {
//Register for notification ...
private func addjustSafeAreaTo(keyboardFrame: CGRect) {
view.safeAreaInsets.bottom = keyboardFrame.height
}
}
UIKeyboardAnimationCurveUserInfoKey: 7
UIKeyboardAnimationDurationUserInfoKey: 0.25
UIKeyboardBoundsUserInfoKey: NSRect: {{0, 0}, {820, 337}}
UIKeyboardCenterBeginUserInfoKey: NSPoint: {410, 1319.5}
UIKeyboardCenterEndUserInfoKey: NSPoint: {410, 1011.5}
UIKeyboardFrameBeginUserInfoKey: NSRect: {{0, 1180}, {820, 279}}
UIKeyboardFrameEndUserInfoKey: NSRect: {{0, 843}, {820, 337}}
UIKeyboardIsLocalUserInfoKey: 1
UIResponder.keyboardWillShowNotification
UIResponder.keyboardDidShowNotification
UIResponder.keyboardWillHideNotification
UIResponder.keyboardDidHideNotification
#!/bin/bash
if [ $# -lt 2 ]; then
echo "format: git_cleanup <repo name> [-x/--expunge <directory 1> -l/--keeplatest <directory 2> ...]"
exit
fi
PROJ="$1"
shift