擷取iOS裝置鍵盤高度,擷取iOS裝置鍵盤

來源:互聯網
上載者:User

擷取iOS裝置鍵盤高度,擷取iOS裝置鍵盤

  最近做了一個自訂鍵盤,首先是要知道iOS裝置各種鍵盤的高度,下面就來說一下怎麼擷取鍵盤的高度。

  主要是利用鍵盤彈出時的通知。

  1、首先先隨便建一個工程。

  2、在工程的 -(void)viewDidload;函數中添加鍵盤彈出和隱藏的通知,具體代碼如下:

 1       //增加監聽,當鍵盤出現或改變時收出訊息 2       [[NSNotificationCenter defaultCenter] addObserver:self 3                                             selector:@selector(keyboardWillShow:) 4                                             name:UIKeyboardWillShowNotification 5                                             object:nil]; 6  7       //增加監聽,當鍵退出時收出訊息 8       [[NSNotificationCenter defaultCenter] addObserver:self 9                                             selector:@selector(keyboardWillHide:)10                                             name:UIKeyboardWillHideNotification11                                             object:nil];

  3、當得到通知時寫2個函數,來響應通知 -(void)keyboardWillShow; -(void)keyboardWillHide;

    在這2個函數中可以得到鍵盤的一些屬性,具體代碼如下:

 1 - (void)keyboardWillShow:(NSNotification *)aNotification 2 { 3     //擷取鍵盤的高度 4     /* 5      iphone 6: 6      中文 7      2014-12-31 11:16:23.643 Demo[686:41289] 鍵盤高度是  258 8      2014-12-31 11:16:23.644 Demo[686:41289] 鍵盤寬度是  375 9      英文10      2014-12-31 11:55:21.417 Demo[1102:58972] 鍵盤高度是  21611      2014-12-31 11:55:21.417 Demo[1102:58972] 鍵盤寬度是  37512      13      iphone  6 plus:14      英文:15      2014-12-31 11:31:14.669 Demo[928:50593] 鍵盤高度是  22616      2014-12-31 11:31:14.669 Demo[928:50593] 鍵盤寬度是  41417      中文:18      2015-01-07 09:22:49.438 Demo[622:14908] 鍵盤高度是  27119      2015-01-07 09:22:49.439 Demo[622:14908] 鍵盤寬度是  41420      21      iphone 5 :22      2014-12-31 11:19:36.452 Demo[755:43233] 鍵盤高度是  21623      2014-12-31 11:19:36.452 Demo[755:43233] 鍵盤寬度是  32024      25      ipad Air:26      2014-12-31 11:28:32.178 Demo[851:48085] 鍵盤高度是  26427      2014-12-31 11:28:32.178 Demo[851:48085] 鍵盤寬度是  76828      29      ipad2 :30      2014-12-31 11:33:57.258 Demo[1014:53043] 鍵盤高度是  26431      2014-12-31 11:33:57.258 Demo[1014:53043] 鍵盤寬度是  76832      */33     NSDictionary *userInfo = [aNotification userInfo];34     NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];35     CGRect keyboardRect = [aValue CGRectValue];36     int height = keyboardRect.size.height;37     int width = keyboardRect.size.width;38     NSLog(@"鍵盤高度是  %d",height);39     NSLog(@"鍵盤寬度是  %d",width);40 }41 42 //當鍵退出時調用43 - (void)keyboardWillHide:(NSNotification *)aNotification44 {45 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.