當cell中有UItextfiled或者UITextVIew時,彈出鍵盤把tableview往上,但是有的cell沒有移動

來源:互聯網
上載者:User

標籤:super   ati   是什麼   point   efault   img   png   textfield   cgpoint   

cell中有UITextView時,輸入文字是需要將tableView向上移,基本的做法是,註冊鍵盤變化的通知在通知的方法中做tableVIew的位置調整,

一,一般做法

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    NSLog(@"register");
}

 

- (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);
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification {

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
}
今天做項目發現以上方法有一定缺陷,當tableView有多組的時候,有一組cell不跟著向上移動照成如下情況:

當鍵盤出現時,尾視圖向上移動了但是這一組中的cell沒有向上移動,這是什麼原因,我至今沒有搞清楚.

上述方法還會出現問題

解決辦法找到了就是在鍵盤出現的通知方法中找到第一響應者(一般是:UITextField),判斷它所在的cell是否顯示在可見地區,如果沒有則讓tableVIew滑動至該cell全部可見:

#pragma mark 鍵盤出現

-(void)keyboardWillShow:(NSNotification *)note

{

    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    //取得第一響應者

    UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];  

    UIView *activeField = [keyWindow performSelector:@selector(firstResponder)]; 

    //除去鍵盤後的頁面顯示地區

    CGRect aRect = self.view.frame;

    aRect.size.height =  aRect.size.height - keyBoardRect.size.height - 64;

    //判斷cell底部是否在顯示範圍內

    CGPoint cellBottomPoint = CGPointMake(0.0, activeField.superview.superview.frame.origin.y +activeField.superview.superview.frame.size.height);

    if (!CGRectContainsPoint(aRect, cellBottomPoint) ) {

        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);

        CGPoint scrollPoint = CGPointMake(0.0, cellBottomPoint.y-aRect.size.height -44);

        [_tableView setContentOffset:scrollPoint animated:YES];

        

    }

    

}

#pragma mark 鍵盤消失

-(void)keyboardWillHide:(NSNotification *)note

{

     

    [UIView animateWithDuration:.35 animations:^{

       self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 45, 0);

    }];

}

這樣就完美解決了以上問題;

 

當cell中有UItextfiled或者UITextVIew時,彈出鍵盤把tableview往上,但是有的cell沒有移動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.