Return to Snippet

Revision: 23138
at August 31, 2010 22:37 by habib123


Updated Code
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];


- (void)keyboardWillShow:(NSNotification *)notification
{
        
    UIScrollView *scrollView = (UIScrollView *)self.view;
    CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGPoint center = [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];

    // We need to compute the keyboard and table view frames in window-relative coordinates
    CGRect keyboardFrame = CGRectMake(round(center.x - bounds.size.width/2.0), round(center.y - bounds.size.height/2.0), bounds.size.width, bounds.size.height);
    CGRect viewFrame = [scrollView.window convertRect:scrollView.frame fromView:scrollView.superview];

    // And then figure out where they overlap
    CGRect intersectionFrame = CGRectIntersection(viewFrame, keyboardFrame);

        // This assumes that no one else cares about the table view's insets...
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, intersectionFrame.size.height, 0);
    [scrollView setContentInset:insets];
    [scrollView setScrollIndicatorInsets:insets];
    [scrollView flashScrollIndicators];

}

- (void)keyboardWillHide:(NSNotification *)notification
{
        
    UIScrollView *scrollView = (UIScrollView *)self.view;
    // This assumes that no one else cares about the table view's insets...
    [scrollView setContentInset:UIEdgeInsetsZero];
    [scrollView setScrollIndicatorInsets:UIEdgeInsetsZero];
         
}

//For iPad
- (void)keyboardWillMove:(NSNotification *)note {
    NSDictionary *info = [note userInfo];
    CGRect kbBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect kbEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    kbBeginFrame = [self.view convertRect:kbBeginFrame fromView:nil];
    kbEndFrame = [self.view convertRect:kbEndFrame fromView:nil];

    double animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve animationCurve = (UIViewAnimationCurve)[[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    [UIView beginAnimations:@"OAKeyboardAnimation" context:nil];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationDuration:animationDuration];

    CGFloat kbYDelta = kbEndFrame.origin.y - kbBeginFrame.origin.y;
    CGFloat x = leftButton.center.x;

    leftButton.center = CGPointMake(x, leftButton.center.y + kbYDelta);
    middleButton.center = CGPointMake(x, middleButton.center.y + kbYDelta);
    rightButton.center = CGPointMake(x, rightButton.center.y + kbYDelta);

    [UIView commitAnimations];
}

Revision: 23137
at January 31, 2010 03:50 by habib123


Initial Code
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];


- (void)keyboardWillShow:(NSNotification *)notification
{
        
    UIScrollView *scrollView = (UIScrollView *)self.view;
    CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGPoint center = [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];

    // We need to compute the keyboard and table view frames in window-relative coordinates
    CGRect keyboardFrame = CGRectMake(round(center.x - bounds.size.width/2.0), round(center.y - bounds.size.height/2.0), bounds.size.width, bounds.size.height);
    CGRect viewFrame = [scrollView.window convertRect:scrollView.frame fromView:scrollView.superview];

    // And then figure out where they overlap
    CGRect intersectionFrame = CGRectIntersection(viewFrame, keyboardFrame);

        // This assumes that no one else cares about the table view's insets...
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, intersectionFrame.size.height, 0);
    [scrollView setContentInset:insets];
    [scrollView setScrollIndicatorInsets:insets];
    [scrollView flashScrollIndicators];

}

- (void)keyboardWillHide:(NSNotification *)notification
{
        
    UIScrollView *scrollView = (UIScrollView *)self.view;
    // This assumes that no one else cares about the table view's insets...
    [scrollView setContentInset:UIEdgeInsetsZero];
    [scrollView setScrollIndicatorInsets:UIEdgeInsetsZero];
         
}

Initial URL

                                

Initial Description

                                

Initial Title
Keyboard Notification

Initial Tags

                                

Initial Language
Objective C