码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取键盘相关信息

时间:2015-04-10 17:00:56      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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

[[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);
    }];
}

 

iOS 获取键盘相关信息

标签:

原文地址:http://www.cnblogs.com/shidaying/p/4414889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!