• iOS 关于键盘监听


    step—1:键盘的监听

    1.一般在viewWillAppear:方法中添加监听事件

    - (void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];

     //增加监听,当键盘出现或改变时收出消息

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

     //增加监听,当键退出时收出消息

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

    }

    //当键盘弹出时调用

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

     //获取键盘的高度

     NSDictionary*userInfo = [notification userInfo];

     NSValue*value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

     CGRectkeyboardRect = [value CGRectValue];

     CGFloatheight = keyboardRect.size.height;

     //第一种 self.view 平移的属性

     //self.view.transform=CGAffineTransformMakeTranslation(0,-height);

     //第二种 self.view frame的属性

     CGRectframe = self.view.frame;

        frame.origin.y= -height;

     self.view.frame= frame;

     NSLog(@"键盘打开...%@",NSStringFromCGRect(self.view.frame));

    }

    //当退出退出时调用

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

     //获取键盘的高度

     NSDictionary*userInfo = [notification userInfo];

     NSValue*value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

     CGRectkeyboardRect = [value CGRectValue];

     CGFloatheight = keyboardRect.size.height;

     //第一种 self.view 平移的属性

     //self.view.transform=CGAffineTransformIdentity;

     //self.view.transform=CGAffineTransformMakeTranslation(0,0);

     //第二种 self.view frame的属性

     CGRectframe = self.view.frame;

         frame.origin.y= 0;

     self.view.frame= frame;

     NSLog(@"键盘关闭...%@",NSStringFromCGRect(self.view.frame));

    }

    2.一般在viewWillDisappear:或dealloc 方法中移除

    - (void)viewWillDisappear:(BOOL)animated{

        [superviewWillDisappear:animated];

     //解除键盘出现通知

        [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];

     //解除键盘隐藏通知

        [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];

    }

    - (void)dealloc{

     //解除键盘出现通知

        [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];

     //解除键盘隐藏通知

        [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];

    }

    Step—2:键盘的的种类

    1>UIKeyboardTypeDefault:

    2>UIKeyboardTypeASCIICapable:

    3>UIKeyboardTypeNumbersAndPunctuation:

    4>UIKeyboardTypeURL:

    5>UIKeyboardTypeNumberPad:

    6>UIKeyboardTypePhonePad:

    7>UIKeyboardTypeNamePhonePad:

    8>UIKeyboardTypeEmailAddress:

    9>UIKeyboardTypeDecimalPad:

    10>UIKeyboardTypeTwitter:

    11>UIKeyboardTypeWebSearch:

    12>UIKeyboardTypeAlphabet:

  • 相关阅读:
    校园篮球网页作业成品 运动系列NBA篮球主题 学校篮球网页制作模板 学生简单体育运动网站设计成品
    【Android 逆向】ART 函数抽取加壳 ③ ( 禁用 dex2oat 操作 HOOK 点介绍 | 集成 InLineHook )
    python语言性能不适合在移动端做图色模拟开发,推荐用lua
    harbor私有仓库部署
    python之Scipy
    肠道菌群代谢组学之粪便微生物移植治疗原发性硬化性胆管炎
    基于多云构建监控告警系统
    警惕Mallox勒索病毒的最新变种malloxx,您需要知道的预防和恢复方法。
    期货开户保证金是一把双刃剑
    新手python爬虫100个入门项目
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126419752