IQKeyboardManager失效,键盘遮挡问题

当你使用这个第三方时 , 有时偶尔会发现失效
当时我在UITableviewCell中添加了一个TextField, 本来IQKeyboardManager是支持UITableview中的键盘处理问题的,但是一开始却想错了方向, 认为cell中添加控件可能层级较深, 框架本身并不支持

所以下面说下解决的几种方式:

一: 先查看

- (void)viewWillAppear:(BOOL)animated
中, 是否有写了[super viewWillAppear:animated];
如果这个调用父类的方法没有写,那么IQKeyboardManager在调用父类控件做位移时就没法调到,那么你的键盘出现时, UITableviewController就不能往上移动

二: 监听键盘弹出

1.首先在- (void)viewDidLoad中调用对键盘实现监听

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

2.然后调用通知的方法:

#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
    self.tableView.contentInset = UIEdgeInsetsZero;
}

三: 按照苹果官方文档实现

思路也是调用通知,但是方法有异, 具体如下:
1.首先在- (void)viewDidLoad中调用对键盘实现监听

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];
    
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

2.然后调用通知的方法:

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.InvestInfocell.activityNumber.frame.origin) ) {
        [self.tableView scrollRectToVisible:self.InvestInfocell.activityNumber.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}


以上接种方式,应该可以解决键盘遮挡问题

推荐阅读更多精彩内容