Created
April 19, 2017 08:53
-
-
Save jeremiegirault/8e9c559f7e72d26581dd8b4149d2d372 to your computer and use it in GitHub Desktop.
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
import UIKit | |
import RxSwift | |
private var disposeBagKey: UInt8 = 0 | |
private final class KeyboardObserver { | |
private static var key: UInt8 = 0 | |
let guide = UILayoutGuide() | |
private var isAttached = false | |
private let top: NSLayoutConstraint | |
private let left: NSLayoutConstraint | |
private let width: NSLayoutConstraint | |
private let height: NSLayoutConstraint | |
static func layoutGuide(for viewController: UIViewController) -> UILayoutGuide { | |
if let observer = objc_getAssociatedObject(viewController, &KeyboardObserver.key) as? KeyboardObserver { return observer.guide } | |
let observer = KeyboardObserver(in: viewController.view) | |
objc_setAssociatedObject(viewController, &KeyboardObserver.key, observer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
return observer.guide | |
} | |
private init(in view: UIView) { | |
view.addLayoutGuide(guide) | |
top = guide.topAnchor.constraint(equalTo: view.topAnchor) | |
left = guide.leadingAnchor.constraint(equalTo: view.leadingAnchor) | |
width = guide.widthAnchor.constraint(equalToConstant: 0) | |
height = guide.heightAnchor.constraint(equalToConstant: 0) | |
// default constraints to bottom | |
NSLayoutConstraint.activate([ | |
guide.widthAnchor.constraint(equalTo: view.widthAnchor).ecx.priority(UILayoutPriorityDefaultLow), | |
guide.heightAnchor.constraint(equalToConstant: 0).ecx.priority(UILayoutPriorityDefaultLow), | |
guide.leadingAnchor.constraint(equalTo: view.leadingAnchor).ecx.priority(UILayoutPriorityDefaultLow), | |
guide.topAnchor.constraint(equalTo: view.bottomAnchor).ecx.priority(UILayoutPriorityDefaultLow) | |
]) | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(keyboardWillChange(notification:)), | |
name: .UIKeyboardWillChangeFrame, | |
object: nil) | |
} | |
@objc private func keyboardWillChange(notification: NSNotification) { | |
guard let view = guide.owningView else { return } | |
let beginFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue | |
let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue | |
let animationDuration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double | |
let animationCurve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt | |
let fromFrameInView = view.convert(beginFrame, from: nil) | |
let toFrameInView = view.convert(endFrame, from: nil) | |
update(fromFrameInView) | |
UIView.animate(withDuration: animationDuration, | |
delay: 0, | |
options: UIViewAnimationOptions(rawValue: animationCurve), | |
animations: { | |
self.update(toFrameInView) | |
}, completion: nil) | |
} | |
private func update(_ rect: CGRect) { | |
top.constant = rect.origin.y | |
left.constant = rect.origin.x | |
width.constant = rect.size.width | |
height.constant = rect.size.height | |
if !isAttached { | |
isAttached = true | |
NSLayoutConstraint.activate([ top, left, width, height ]) | |
} | |
guide.owningView?.layoutIfNeeded() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment