ios 鍵盤高度及鍵盤響應事件

來源:互聯網
上載者:User

背景:

  ios5之前,iphone上的鍵盤的高度是固定為216.0px高的,中文漢字的選擇框是懸浮的,所以不少應用都將此高度來標註鍵盤的高度。

  可是在ios5中,鍵盤配置變了,尤其是中文輸入時,中文漢字選擇框就固定在鍵盤上方,這樣就使得原本與鍵盤緊密貼合的介面視圖被中文漢字選擇框給覆蓋住了。一方面影響了介面的美觀,另一方面,如果被覆蓋的部分就是文本輸入框的話,使用者就無法看到輸入的內容了。因此這個問題就必須得解決了。

解決方案:

  其實在一開始使用216.0px這個固定值來標註鍵盤的高度就是錯誤的。因為在ios3.2以後的系統中,蘋果就提供了鍵盤使用的api以及demo程式——“KeyboardAccessory”。

  處理鍵盤事件的正確方法是這樣的:(包括擷取鍵盤的高度以及鍵盤彈出和消失動畫的時間)

1)在要使用鍵盤的視圖控制器中,接收鍵盤事件的通知:

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

// 鍵盤高度變化通知,ios5.0新增的
#ifdef __IPHONE_5_0
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#endif


  2)然後添加鍵盤事件的處理代碼:

    擷取到當前keyboard的高度以及動畫時間,然後對視圖進行對應的操作即可。

#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {

/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/

NSDictionary *userInfo = [notification userInfo];

// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];

// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
}


- (void)keyboardWillHide:(NSNotification *)notification {

NSDictionary* userInfo = [notification userInfo];

/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

[self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
}

  3)在視圖控制器消除時,移除鍵盤事件的通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];



ps:

  ios5隱藏功能分享——“字典”功能(英英字典):

  在任何輸入框中選中一個英文單詞,此時會有選擇項“複製”,“刪除”...等,還有一個向右的箭頭,點擊這個向右的箭頭後,就會出現“定義”選項,點擊這個“定義”按鈕即會彈出這個英語單詞的英文解釋。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.