Listen to the keyboard pop-up hidden state, and the view changes its height accordingly

add listener

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

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

 remove monitor

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

 event handler

- (void) changeContentViewPosition:(NSNotification *)notification{

    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyBoardEndY = value.CGRectValue.origin.y;

    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:duration.doubleValue animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:[curve intValue]];
        self.view.center = CGPointMake(self.view.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - self.view.bounds.size.height/2.0);
    }];
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326744735&siteId=291194637