美文网首页iOS开发
iOS开发-键盘高度

iOS开发-键盘高度

作者: BestVast | 来源:发表于2017-09-13 14:14 被阅读422次

一、监听键盘出行和消失

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardHideOrShow:(NSNotification *)notification {
    NSString *notificationName = notification.name;     //获取通知名称
    NSDictionary *keyboardInfo = notification.userInfo;//获取通知内容
    /*
     NSLog(@"keyboardInfo: %@", keyboardInfo);
     键盘将要弹起时打印 keyboardInfo: {
     UIKeyboardAnimationCurveUserInfoKey = 7;
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 270}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 827}";
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 270}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 692}, {414, 270}}";
     UIKeyboardIsLocalUserInfoKey = 1;
     }
     键盘将要消失时打印 keyboardInfo: {
     UIKeyboardAnimationCurveUserInfoKey = 7;
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 270}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 827}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 871}";
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 692}, {414, 270}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 736}, {414, 270}}";
     UIKeyboardIsLocalUserInfoKey = 1;
     }
     */
    CGRect keyboardFrame = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];     
    //keyboardFrame.size.height 为键盘的高度
    if ([notificationName isEqualToString:UIKeyboardWillHideNotification]) {
        //键盘将要消失
    }
    else {
        //键盘将要弹起
    }
}

二、禁止UIWebView随键盘的弹起而往上滚动 借鉴于此,谢谢

问题:当UIWebView中的html有输入框,点击输入框,UIWebView会随键盘的弹起而整体往上移动,收起键盘后,UIWebView无法回到原来的位置;
问题的原因:由于UIWebView继承的是UIScrollerView,因此,当键盘弹起时,UIScrollerView的content会整体往上移动;

解决问题的方案:
<UIScrollViewDelegate>
_webView.scrollView.delegate = self;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return nil;
}

相关文章

网友评论

    本文标题:iOS开发-键盘高度

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