美文网首页
iOS 监控键盘高度

iOS 监控键盘高度

作者: 流老湿 | 来源:发表于2018-01-27 15:52 被阅读22次

- (void)registerForKeyboardNotifications

{

    //使用NSNotificationCenter 键盘弹出时

    [[NSNotificationCenter defaultCenter] addObserver:self

                                            selector:@selector(keyboardWillShown:)

                                                name:UIKeyboardWillChangeFrameNotification object:nil];

    //使用NSNotificationCenter 键盘隐藏时

    [[NSNotificationCenter defaultCenter] addObserver:self

                                            selector:@selector(keyboardWillBeHidden:)

                                                name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWillShown:(NSNotification*)aNotification

{

    NSDictionary *info = [aNotification userInfo];

    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    //输入框位置动画加载

    [UIView animateWithDuration:duration animations:^{

        //do something

    }];

}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    NSDictionary *info = [aNotification userInfo];

    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [UIView animateWithDuration:duration animations:^{

        //do something

    }];

}

相关文章

网友评论

      本文标题:iOS 监控键盘高度

      本文链接:https://www.haomeiwen.com/subject/uqwnaxtx.html