iOS 键盘遮挡问题的完美解决

项目中我们经常用IQKeyboardManager,这个也可以解决大部分的键盘遮挡问题,但是呢,如果出现在弹窗上的输入框IQKeyboardManager就很难控制了,也自然会出现键盘遮挡问题。那么下面的这个第三方就可以完美的解决弹窗上的输入框的键盘遮挡问题:
TPKeyboardAvoiding :https://github.com/michaeltyson/TPKeyboardAvoiding
这是一款通用型的适配方式。

集成也很方便,1,pod导入,2,手动拖到自己项目中
使用也比较简单,具体代码步骤如下:
1,在弹窗页面上添加属性:
@property (nonatomic, retain) TPKeyboardAvoidingScrollView *scrollView;

2,懒加载:

  • (TPKeyboardAvoidingScrollView *)scrollView{
    if (!_scrollView) {
    _scrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    _scrollView.scrollEnabled = NO;
    _scrollView.canCancelContentTouches = YES;
    _scrollView.delaysContentTouches = NO;
    // _scrollView.userInteractionEnabled = NO;
    }
    return _scrollView;
    }

3,弹窗显示的时候添加到window上:

  • (void)show {
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self setBackgroundColor:[UIColor clearColor]];
    [self.scrollView addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.scrollView];
    [self.textView becomeFirstResponder];
    }
    4,弹窗消失的时候,从父类中移除:
  • (void)dissmiss {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self layoutIfNeeded];
    WeakSelf
    [UIView animateWithDuration:0.2
    animations:^{
    [weakSelf.scrollView removeFromSuperview];
    }];
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容