ios 弹出键盘 视图向上平移


    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    
}

-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
    return keyboardEndingFrame.size.height;
}

-(void)keyboardwillAppear:(NSNotification *)notification
{
    CGRect currentFrame = self.view.frame;
    NSLog(@"currentFrame.origin.y: %f",currentFrame.origin.y);
    if (currentFrame.origin.y!=0.00) {
        return;
    }
//    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = -60 ;
    self.view.frame = currentFrame;
}

-(void)keyboardWillDisappear:(NSNotification *)notification
{
    CGRect currentFrame = self.view.frame;
//    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = 0.0;//currentFrame.origin.y + change ;
    self.view.frame = currentFrame;
}

你可能感兴趣的:(ios 弹出键盘 视图向上平移)