Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned Up KeyboardLayoutConstraint #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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