2015-01-04 89 views
0

我在Main.Storyboard中創建的UIViewController內有TableView。 初始化時Y位置爲0。 我的問題是,當鍵盤顯示時,TableView的Y位置根本不會改變。 Y位置保持爲0.因此單元格隱藏在NavigationController後面。在鍵盤顯示時在UIViewController中設置Tableview的y位置

至今代碼:

-(void) keyboardWasShown:(NSNotification*)aNotification 
{ 
    if (kbIsShown == false) { 
     NSDictionary* info = [aNotification userInfo]; 
     NSTimeInterval animationDuration; 
     UIViewAnimationCurve animationCurve; 
     CGRect keyboardFrame; 
     [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
     [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
     [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame]; 

     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:animationDuration]; 
     [UIView setAnimationCurve:animationCurve]; 
     NSLog(@"frame..%f..%f..%f..%f",self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height); 
     NSLog(@"keyboard..%f..%f..%f..%f",keyboardFrame.origin.x, keyboardFrame.origin.y, keyboardFrame.size.width, keyboardFrame.size.height); 
     [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; 
     [chatTable setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height + TABBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height-keyboardFrame.size.height)]; 
     [UIView commitAnimations]; 
     NSLog(@"Visar sig"); 
     kbIsShown = true; 
    } 
} 

[chatTable setFrame:]沒有做任何事情。​​Y位置保持爲0.如何更改Tableview的Y位置以便我的內容可見?

注意:[self.view setFrame...]確實會改變視圖的Y位置。但chatTable停留在0

+1

http://stackoverflow.com/questions/13845426/generic-uitableview-keyboard-resizing-algorithm – rakeshbs 2015-01-04 13:44:37

+0

Thanks @rakeshbs!我設法使它工作!我做了一些小的改變! – Dridia 2015-01-04 15:23:22

+0

不客氣。 – rakeshbs 2015-01-04 15:24:01

回答

0

爲了您的滾動表格視圖所需行使用:

[chatTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

你還應該設置:

chatTable.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);