iOS 获取键盘相关信息

一,在需要的地方添加监听

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

 二,响应监听方法

- (void)onKeyboardWillShowNotification:(NSNotification*)notify {
    NSDictionary *userInfoDic = [notify userInfo];
    NSLog(@"userInfoDic = %@", userInfoDic);
    CGFloat timerval = [[userInfoDic objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    NSValue *keyboardFrameValue = [userInfoDic objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
    [UIView animateWithDuration:timerval animations:^(void){
        self.containerView.frame = CGRectMake(self.containerView.frame.origin.x, self.containerView.frame.origin.y - keyboardFrame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height);
    }];
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。