iOS開發(fā)-鍵盤高度

一、監(jiān)聽鍵盤出行和消失

[[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;//獲取通知內(nèi)容
    /*
     NSLog(@"keyboardInfo: %@", keyboardInfo);
     鍵盤將要彈起時(shí)打印 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;
     }
     鍵盤將要消失時(shí)打印 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隨鍵盤的彈起而往上滾動 借鑒于此,謝謝

問題:當(dāng)UIWebView中的html有輸入框,點(diǎn)擊輸入框,UIWebView會隨鍵盤的彈起而整體往上移動,收起鍵盤后,UIWebView無法回到原來的位置;
問題的原因:由于UIWebView繼承的是UIScrollerView,因此,當(dāng)鍵盤彈起時(shí),UIScrollerView的content會整體往上移動;

解決問題的方案:
<UIScrollViewDelegate>
_webView.scrollView.delegate = self;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return nil;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,245評論 4 61
  • 努力有那么難嘛?堅(jiān)持有那么難嘛?
    WinMini閱讀 108評論 0 0
  • 近日看到一朋友寫的日志,說“寫作和旅行是兩個很好的養(yǎng)生辦法,因?yàn)樗鼈兌际窃谑崂盱`魂。肉體是人的硬件,靈魂是人的軟件...
    自在牛閱讀 742評論 0 8
  • H3C 有個叫 UISM 的產(chǎn)品,有點(diǎn)意思,把 x86 放到了交換機(jī)里頭, 成了機(jī)架內(nèi)基礎(chǔ)設(shè)施的管理平臺,能夠管理...
    Alick閱讀 3,874評論 0 0