首页 > 代码库 > 键盘上升下降

键盘上升下降

//当键盘出现或改变时调用

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = http://www.mamicode.com/[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    CGFloat height = keyboardRect.size.height;

    CGFloat offset = CGRectGetMaxY(self.urlInput.frame) + 10 - (CGRectGetMaxY(self.view.bounds) - height);

    

    NSNumber *durationNum = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    CGFloat duration = [durationNum floatValue];

    

    if (offset > 0)

    {

        [UIView animateWithDuration:duration animations:^{

            self.view.frame = CGRectMake(0,-offset, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

        }];

    }

}

 

//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    NSDictionary *userInfo = [aNotification userInfo];

    NSNumber *durationNum = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    CGFloat duration = [durationNum floatValue];

    

    [UIView animateWithDuration:duration animations:^{

        self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

    }];

}

 

键盘上升下降