iOS_经验(6)_Keyboard_高度处理

一丶键盘问题

1.解决键盘弹起,挡住文本框的问题;

二丶解决方案:

① 简易处理

第三方,一句话搞定;

IQKeyboardManager

其他博客详细介绍:
//www.greatytc.com/p/9d7d246bd350/comments/1518291

使用:
#import <IQKeyboardManager.h>

② 自定义化处理
采用NSNotification

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//监听键盘改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//取消监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

#pragma mark - notification
- (void)KeyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect tmpViewRect = self.view.frame;
    if (endKeyboardRect.origin.y == [UIScreen height])
    {
        //下降
        tmpViewRect.origin.y = 0;
    }
    else
    {
        //上升
        CGFloat maxY = CGRectGetMaxY(self.backView.frame);
        CGFloat spaceH = [UIScreen height] - maxY;
        CGFloat keyboardH = endKeyboardRect.size.height;
        if (spaceH < keyboardH) {
            tmpViewRect.origin.y = - (keyboardH - spaceH);
        }
    }
     self.view.frame = tmpViewRect;
}

三丶注意事项

1.添加监听者,和取消监听者写的位置在生命周期上是相对的;
2.别忘记取消监听;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容