博客> 监测键盘弹出,textfiled的上移
监测键盘弹出,textfiled的上移
1小时前 评论:0 阅读:96 suifumin
textFiled 键盘监测

首先注册两个通知来监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];

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

然后分别实现

pragma mark -键盘即将显示的时候调用

  • (void)keyboardWillShow:(NSNotification *)sender{

    //获取键盘高度, CGFloat kbHeight = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    // 取得键盘的动画时间,这样可以在视图上移的时候更连贯 double duration = [[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    NSLog(@"%f---%f",kbHeight,duration);

    [UIView animateWithDuration:duration animations:^{ CGRect frame = self.activityView.frame; frame.origin.y = SCREEN_HEIGHT- kbHeight - 250; self.activityView.frame = frame; }];

}

pragma mark - 键盘退出的时候调用

-(void)keyboardWillHidden:(NSNotification *)sender{ [UIView animateWithDuration:0.25 animations:^{ CGRect frame = self.activityView.frame; frame.origin.y = SCREEN_HEIGHT - 250; self.activityView.frame = frame; }];

}

收藏
0
sina weixin mail 回到顶部