IQKeyboardManager tablview中TextField失效

可能原因
1.設置了tableView.contentInset,所以需要重新設置更改
2.父視圖是個 scrollView ,卻忘記了設置contentSize,
加上contentSize,纔會起作用,整個頁面纔會上移

下面說下解決的幾種方式:
一: 先查看
- (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;
}

原文:IQKeyboardManager失效,鍵盤遮擋問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章