美文网首页iOS开发-键盘
iOS键盘通知代码最佳实践

iOS键盘通知代码最佳实践

作者: Realank | 来源:发表于2018-08-01 10:15 被阅读92次

    每次用到键盘通知都从老代码里找,太麻烦了,总结一下:
    首先在页面启动的时候加载通知

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

    再在dealloc时移除通知

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    键盘通知的接收方法:

    #pragma mark - UIKeyboardNotification
    - (void)keyboardWillShow:(NSNotification *)noti{
        CGFloat keyboardHeight = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    }
    
    
    - (void)keyboardWillHide:(NSNotification *)noti{
        CGFloat keyboardHeight = 0;
    }
    
    

    相关文章

      网友评论

        本文标题:iOS键盘通知代码最佳实践

        本文链接:https://www.haomeiwen.com/subject/slslvftx.html