监听键盘的弹出和收回通知

- (void)viewDidLoad
{
    [super viewDidLoad];

    //增加监听,当键盘出现或改变时触发方法
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    //增加监听,当键退出时调用代理方法
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];    
}

 //当键盘出现或改变时回调的代理方法
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
   int height = keyboardRect.size.height;
}

//当键退出时回调
- (void)keyboardWillHide:(NSNotification *)aNotification{}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 吴启华 1964年5月19日出生于香港,籍贯广东省番禺,演员。1983年签约香港无线电视台后,在《魔域桃源》中担任...
    大白仟佰黛阅读 726评论 1 0
  • 哇塞,好棒啊!孩子起床了,刚才说让她5分钟起床后吃饭。孩子起来啦!我问孩子现在起床,还是5分钟后妈妈再来叫你。孩子...
    羽者8227阅读 405评论 1 4
  • 1.装饰者模式的简介 装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰者到相比子类更加灵活 装饰...
    Mrsunup阅读 7,290评论 1 1

友情链接更多精彩内容