Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 39 additions & 60 deletions Spring/KeyboardLayoutConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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