diff --git a/Spring/KeyboardLayoutConstraint.swift b/Spring/KeyboardLayoutConstraint.swift index 7d1cf9c..3f6bea3 100644 --- a/Spring/KeyboardLayoutConstraint.swift +++ b/Spring/KeyboardLayoutConstraint.swift @@ -23,88 +23,67 @@ import UIKit #if !os(tvOS) -@available(tvOS, unavailable) public class KeyboardLayoutConstraint: NSLayoutConstraint { + private var offset: CGFloat = 0 - private var offset : CGFloat = 0 - private var keyboardVisibleHeight : CGFloat = 0 + private var visibleKeyboardHeight: CGFloat = 0 { + didSet { + constant = offset + visibleKeyboardHeight + } + } - @available(tvOS, unavailable) override public func awakeFromNib() { super.awakeFromNib() offset = constant - NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillHideNotification(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillShowNotification), + name: .UIKeyboardWillShow, + object: nil + ) + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillHideNotification), + name: .UIKeyboardWillHide, + object: nil + ) } deinit { NotificationCenter.default.removeObserver(self) } - // MARK: Notification - @objc func keyboardWillShowNotification(_ notification: Notification) { - if let userInfo = notification.userInfo { - if let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue { - let frame = frameValue.cgRectValue - keyboardVisibleHeight = frame.size.height - } - - self.updateConstant() - switch (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber) { - case let (.some(duration), .some(curve)): - - let options = UIViewAnimationOptions(rawValue: curve.uintValue) - - UIView.animate( - withDuration: TimeInterval(duration.doubleValue), - delay: 0, - options: options, - animations: { - UIApplication.shared.keyWindow?.layoutIfNeeded() - return - }, completion: { finished in - }) - default: - - break - } - - } + guard let userInfo = notification.userInfo else { return } + + guard let frame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return } + visibleKeyboardHeight = frame.size.height + animateChanges(accordingTo: userInfo) } @objc func keyboardWillHideNotification(_ notification: NSNotification) { - keyboardVisibleHeight = 0 - self.updateConstant() + guard let userInfo = notification.userInfo else { return } - if let userInfo = notification.userInfo { - - switch (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber) { - case let (.some(duration), .some(curve)): - - let options = UIViewAnimationOptions(rawValue: curve.uintValue) - - UIView.animate( - withDuration: TimeInterval(duration.doubleValue), - delay: 0, - options: options, - animations: { - UIApplication.shared.keyWindow?.layoutIfNeeded() - return - }, completion: { finished in - }) - default: - break - } - } + visibleKeyboardHeight = 0 + + animateChanges(accordingTo: userInfo) } - func updateConstant() { - self.constant = offset + keyboardVisibleHeight + private func animateChanges(accordingTo userInfo: [AnyHashable: Any]) { + guard + let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval, + let options = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt + else { return } + + UIView.animate( + withDuration: duration, + delay: 0, + options: UIViewAnimationOptions(rawValue: options), + animations: UIApplication.shared.keyWindow!.layoutIfNeeded + ) } - } #endif