-
-
Save hanishassim/d5ddc5eb1c752d041df42f75f346d6a3 to your computer and use it in GitHub Desktop.
A Keyboard helper for your Swift Keyboard troubles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment