Skip to content

Instantly share code, notes, and snippets.

@shepting
Last active March 22, 2016 02:50
Show Gist options
  • Select an option

  • Save shepting/6025439 to your computer and use it in GitHub Desktop.

Select an option

Save shepting/6025439 to your computer and use it in GitHub Desktop.

Revisions

  1. shepting revised this gist Jul 18, 2013. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions YMKeyboardLayoutHelperView.m
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,8 @@ - (id)init
    if (self) {
    self.translatesAutoresizingMaskIntoConstraints = NO;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:@"UIKeyboardWillHideNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
    }
    @@ -32,9 +32,9 @@ - (void)keyboardWillShow:(NSNotification *)notification
    {
    // Save the height of keyboard and animation duration
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardRect = [userInfo[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
    CGRect keyboardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.desiredHeight = CGRectGetHeight(keyboardRect);
    self.duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    self.duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [self animateSizeChange];
    }
    @@ -66,4 +66,4 @@ - (void)dealloc
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

    @end
    @end
  2. shepting revised this gist Jul 18, 2013. 1 changed file with 10 additions and 16 deletions.
    26 changes: 10 additions & 16 deletions YMKeyboardLayoutHelperView.m
    Original file line number Diff line number Diff line change
    @@ -36,40 +36,34 @@ - (void)keyboardWillShow:(NSNotification *)notification
    self.desiredHeight = CGRectGetHeight(keyboardRect);
    self.duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];

    [self setNeedsUpdateConstraints];
    [self animateSizeChange];
    }

    - (void)keyboardWillHide:(NSNotification *)notification
    {
    // Reset the desired height (keep the duration)
    self.desiredHeight = 0.0f;

    [self setNeedsUpdateConstraints];
    [self animateSizeChange];
    }

    - (void)updateConstraints
    - (CGSize)intrinsicContentSize
    {
    [super updateConstraints];

    // Remove old constraints
    if ([self.constraints count]) {
    [self removeConstraints:self.constraints];
    }

    // Add new constraint with desired height
    NSString *constraintFormat = [NSString stringWithFormat:@"V:[self(%f)]", self.desiredHeight];
    [self addVisualConstraints:constraintFormat views:@{@"self": self}];
    return CGSizeMake(UIViewNoIntrinsicMetric, self.desiredHeight);
    }

    - (void)animateSizeChange
    {
    [self invalidateIntrinsicContentSize];

    // Animate transition
    [UIView animateWithDuration:self.duration animations:^{
    [self.superview layoutIfNeeded];
    }];

    }

    - (void)dealloc
    {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

    @end
    @end
  3. shepting created this gist Jul 17, 2013.
    75 changes: 75 additions & 0 deletions YMKeyboardLayoutHelperView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    //
    // YMKeyboardLayoutHelperView.m
    // ios-chat
    //
    // Created by Steven Hepting on 7/17/13.
    // Copyright (c) 2013 Yammer. All rights reserved.
    //

    #import "YMKeyboardLayoutHelperView.h"
    #import "UIView+LayoutAdditions.h"

    @interface YMKeyboardLayoutHelperView ()
    @property (nonatomic) CGFloat desiredHeight;
    @property (nonatomic) CGFloat duration;
    @end

    @implementation YMKeyboardLayoutHelperView

    - (id)init
    {
    self = [super init];
    if (self) {
    self.translatesAutoresizingMaskIntoConstraints = NO;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:@"UIKeyboardWillHideNotification" object:nil];
    }
    return self;
    }

    - (void)keyboardWillShow:(NSNotification *)notification
    {
    // Save the height of keyboard and animation duration
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardRect = [userInfo[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
    self.desiredHeight = CGRectGetHeight(keyboardRect);
    self.duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];

    [self setNeedsUpdateConstraints];
    }

    - (void)keyboardWillHide:(NSNotification *)notification
    {
    // Reset the desired height (keep the duration)
    self.desiredHeight = 0.0f;

    [self setNeedsUpdateConstraints];
    }

    - (void)updateConstraints
    {
    [super updateConstraints];

    // Remove old constraints
    if ([self.constraints count]) {
    [self removeConstraints:self.constraints];
    }

    // Add new constraint with desired height
    NSString *constraintFormat = [NSString stringWithFormat:@"V:[self(%f)]", self.desiredHeight];
    [self addVisualConstraints:constraintFormat views:@{@"self": self}];

    // Animate transition
    [UIView animateWithDuration:self.duration animations:^{
    [self.superview layoutIfNeeded];
    }];

    }

    - (void)dealloc
    {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

    @end