2016-11-09 25 views
0

有關鍵盤出現時移動UITableView的許多答案,但我的問題是不同的。我可以在出現鍵盤時移動UITableView,但是我面臨的唯一問題是當我第一次訪問視圖時,不會滾動,但是當我按回按鈕並重新訪問視圖時,它的工作正常。我很困惑這是每次都期望我第一次訪問視圖時使用的相同代碼。我在xib中使用UITableView。我嘗試設置contentInset但沒用。任何幫助將非常感激。當第一次啓動視圖時出現鍵盤時,uitableview不會滾動。後來它正常工作

我張貼這說明我面臨

enter image description here

問題的圖像下面是我的代碼。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

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


     //tap on tableview or whole view 
     UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; 
     [self.view addGestureRecognizer:gestureRecognizer]; 

     //scroll to bottom of conversation 
     [self scrollToBottomOfConversation]; 

    } 


    - (void)scrollToBottomOfConversation 
    { 
     CGFloat yOffset = 0; 

     if (_chatConversationTableView.contentSize.height > _chatConversationTableView.bounds.size.height) { 
      yOffset = _chatConversationTableView.contentSize.height - _chatConversationTableView.bounds.size.height; 
     } 

     [_chatConversationTableView setContentOffset:CGPointMake(0, yOffset) animated:NO]; 
    } 

    - (void)keyboardWillShow:(NSNotification *)notification 
    { 

     NSDictionary *userInfo = [notification userInfo]; 
     CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
     CGRect frame = CGRectMake(_chatConversationTableView.frame.origin.x, 
            _chatConversationTableView.frame.origin.y, 
            _chatConversationTableView.frame.size.width, 
            _chatConversationTableView.frame.size.height - size.height); 
     _chatConversationTableView.frame = frame; 


     CGRect framee = containerView.frame;//containerView is my subView which holds the textbox and send button 
     framee.origin.y = self.view.frame.size.height - framee.size.height - size.height; 
     containerView.frame = framee; 
     [UIView commitAnimations]; 

     //scroll to bottom of conversation 
     [self scrollToBottomOfConversation]; 


    } 


    -(void)keyboardWillHide:(NSNotification *)notification 
    { 

     NSDictionary *userInfo = [notification userInfo]; 
     CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
     _chatConversationTableView.frame = CGRectMake(_chatConversationTableView.frame.origin.x, 
              _chatConversationTableView.frame.origin.y, 
              _chatConversationTableView.frame.size.width, 
              _chatConversationTableView.frame.size.height + size.height); 

     CGRect frame = containerView.frame; 
     frame.origin.y = self.view.frame.size.height - frame.size.height; 
     containerView.frame = frame; 
     [UIView commitAnimations]; 



    } 
+0

在viewDidAppear方法中寫入「[self scrollToBottomOfConversation]」,而不是viewDidLoad方法。 –

+0

@yagneshdobariya我已經嘗試過,但沒有用。 – Madhu

+0

當鍵盤出現時,由於鍵盤隱藏表,所以更改了表格的底部約束... –

回答

0

在主線程中運行鍵盤通知代碼解決了我的問題。這幫助我解決了這個問題。我認爲出於某種原因,鍵盤出現時,tableview框架的大小沒有更新。發佈代碼以便可以幫助他人。

- (void)keyboardWillShow:(NSNotification *)notification 
    { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //This code will run in the main thread: 

    NSDictionary *userInfo = [notification userInfo]; 
    CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    CGRect frame = CGRectMake(_chatConversationTableView.frame.origin.x, 
           _chatConversationTableView.frame.origin.y, 
           _chatConversationTableView.frame.size.width, 
           _chatConversationTableView.frame.size.height - size.height); 
    _chatConversationTableView.frame = frame; 


    CGRect framee = containerView.frame; 
    framee.origin.y = self.view.frame.size.height - framee.size.height - size.height; 
    containerView.frame = framee; 
    [UIView commitAnimations]; 

     //scroll to bottom of conversation 
     [self scrollToBottomOfConversation]; 


     }); 
} 
+0

它工作正常嗎? –

+0

它的工作正常。 – Madhu

相關問題