Last active
August 29, 2015 14:07
-
-
Save danielctull/abd046a754199ef15b52 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
| @interface ASRKeyboardNotificationValues : NSObject | |
| - (instancetype)initWithNotification:(NSNotification *)notification; | |
| @property (nonatomic, readonly) NSNotification *notification; | |
| @property (nonatomic, readonly) NSTimeInterval animationDuration; | |
| @property (nonatomic, readonly) UIViewAnimationCurve animationCurve; | |
| @property (nonatomic, readonly) UIViewAnimationOptions animationOptions; | |
| @property (nonatomic, readonly) CGRect frameBegin; | |
| @property (nonatomic, readonly) CGRect frameEnd; | |
| @end |
This file contains hidden or 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 "ASRKeyboardNotificationValues.h" | |
| @implementation ASRKeyboardNotificationValues | |
| - (instancetype)initWithNotification:(NSNotification *)notification { | |
| self = [self init]; | |
| if (!self) return nil; | |
| _notification = notification; | |
| return self; | |
| } | |
| - (NSTimeInterval)animationDuration { | |
| return [self.notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; | |
| } | |
| - (UIViewAnimationCurve)animationCurve { | |
| return [self.notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]; | |
| } | |
| - (UIViewAnimationOptions)animationOptions { | |
| return [self animationCurve] << 16; | |
| } | |
| - (CGRect)frameBegin { | |
| return [self frameForKey:UIKeyboardFrameBeginUserInfoKey]; | |
| } | |
| - (CGRect)frameEnd { | |
| return [self frameForKey:UIKeyboardFrameEndUserInfoKey]; | |
| } | |
| - (CGRect)frameForKey:(NSString *)key { | |
| NSValue *value = self.notification.userInfo[key]; | |
| if ([value isKindOfClass:[NSValue class]]) { | |
| return value.CGRectValue; | |
| } | |
| return CGRectNull; | |
| } | |
| @end |
This file contains hidden or 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
| class SomeViewController : UIViewController { | |
| @IBOutlet weak var bottomConstraint: NSLayoutConstraint! | |
| func keyboardWillChangeFrameNotification(notification: NSNotification) { | |
| let values = ASRKeyboardNotificationValues(notification: notification) | |
| self.bottomConstraint.constant = values.frameEnd.size.height | |
| UIView.animateWithDuration(values.animationDuration, delay:0, options:values.animationOptions, animations: { | |
| self.view.layoutIfNeeded() | |
| }, completion: nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment