zoukankan      html  css  js  c++  java
  • 获取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:UIKeyboardWillHideNotification
    11                                             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] 键盘高度是  216
    11      2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是  375
    12      
    13      iphone  6 plus:
    14      英文:
    15      2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是  226
    16      2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是  414
    17      中文:
    18      2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是  271
    19      2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是  414
    20      
    21      iphone 5 :
    22      2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是  216
    23      2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是  320
    24      
    25      ipad Air:
    26      2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是  264
    27      2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是  768
    28      
    29      ipad2 :
    30      2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是  264
    31      2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是  768
    32      */
    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 *)aNotification
    44 {
    45 }
  • 相关阅读:
    [009]类型转换
    [008]new、delete及动态内存分配
    [007]操作符的求解顺序
    [010]转+修正---C++的贪吃蛇程序(未用面向对象封装)
    [006]为什么C++会被叫做是C++?
    [005]逗号表达式
    [JavaScript]转--如何让JS代码高大上
    [009]C---关于输出文本的打印问题
    [008]C---gcc环境下的一个编译器版本问题
    PlayMaker 状态机FSM重用
  • 原文地址:https://www.cnblogs.com/duzj-1990/p/4220456.html
Copyright © 2011-2022 走看看