ios牛叉的demo首页投稿(暂停使用,暂停投稿)

iOS_经验(7)_Keyboard_高度处理

2016-08-14  本文已影响749人  丶纳凉

一丶键盘问题

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

二丶解决方案:

① 简易处理

第三方,一句话搞定;

IQKeyboardManager

其他博客详细介绍:
http://www.jianshu.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.别忘记取消监听;

上一篇下一篇

猜你喜欢

热点阅读