Skip to content

Instantly share code, notes, and snippets.

@louisdh
Forked from matthiasnys/NYSKeyboardHelper.swift
Last active March 10, 2023 21:47
Show Gist options
  • Save louisdh/d69421e8ea26f6281de6c4c823c1283a to your computer and use it in GitHub Desktop.
Save louisdh/d69421e8ea26f6281de6c4c823c1283a to your computer and use it in GitHub Desktop.
A Keyboard helper for your Swift Keyboard troubles.
//
// NYSKeyboardHelper.swift
// B-NYS GCV
//
// Created by Matthias Nys on 18/03/2017.
// Copyright © 2017 B-NYS. All rights reserved.
//
import UIKit
class NYSKeyboardHelper: NSLayoutConstraint {
@IBInspectable var extraIndent: CGFloat = 0.0
override init() {
super.init()
setup()
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
private func setup() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardDidShow, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardDidShow, object: nil)
}
@objc private func keyboardDidChangeVisible(notification: Notification) {
guard let firstItem = self.firstItem as? UIView else {
print("NYSKEYBOARDHELPER: Fist item needs to be a UIView")
return
}
guard let superview = firstItem.superview else {
print("NYSKEYBOARDHELPER: Fist item needs a superview of the type UIView")
return
}
let userInfo = notification.userInfo
var animationDuration: TimeInterval = 0.0
var animationOptions: UIViewAnimationOptions = []
var endFrame = CGRect.zero
if let animationCurve = userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UIViewAnimationCurve {
animationOptions = UIViewAnimationOptions(curve: animationCurve)
}
if let animationDurationFromUserInfo = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval {
animationDuration = animationDurationFromUserInfo
}
if let endFrameFromUserInfo = userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
endFrame = endFrameFromUserInfo
}
let contentAbsoluteFrame = superview.convert(firstItem.frame, to: nil) //To nil -> On the Window
let offset = contentAbsoluteFrame.maxY - endFrame.minY + extraIndent
let keyboardHeight = max(0, offset)
animationOptions.update(with: .layoutSubviews)
UIView .animate(withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: {
self.constant = keyboardHeight
firstItem.layoutIfNeeded()
}, completion: nil)
}
}
extension UIViewAnimationOptions {
init(curve: UIViewAnimationCurve) {
switch curve {
case .easeIn:
self = .curveEaseIn
case .easeOut:
self = .curveEaseOut
case .easeInOut:
self = .curveEaseInOut
case .linear:
self = .curveLinear
}
}
}
@VinceBurn
Copy link

The conversion from UIViewAnimationCurve to UIViewAnimationOptions won't work with iOS Keyboard, since the keyboard is using an undocumented UIViewAnimationCurve value that don't match any value of the UIViewAnimationCurve enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment