我一直這樣做。我這樣做的方式是我有一個被稱爲在UIControlEventEditingDidBegin的文本字段的方法,並在該方法中,我做的:
-(void)startEdit:(UITextField *)textField {
self.prevOffset = self.tableView.contentOffset.y; //I like storing the current offset so I can restore it when the text stops editing, you don't have to do this.
int offSet = [textField superview].frame.origin.y; //this gets the y coordinate of the cell the textField is in. If the table is not at 0,0, you also need to add [[textField superview] superview].frame.origin.y;
offSet-=(self.view.frame.size.height-KEYBOARD_HEIGHT)/2; //where KEYBOARD_HEIGHT is 216 in portrait and 160 in landscape;
if (offSet<0) offSet = 0;
[UIView animateWithDuration:0.3 animations:^{
[self.tableView setContentOffset:CGPointMake(0,offSet)];}];
}
我做了很多其他的事情爲好,但我相信他們特定於我的應用程序。首先,如果偏移量大於0,我將contentInset設置爲UIEdgeInsetsMake(0,0,KEYBOARD_HEIGHT,0),因爲在做這些事之前我有一些跳動的scrollViews。另外,如果原始偏移量(self.prevOffset)加上幀的高度大於內容大小(這也會導致跳躍,因爲它將偏移設置得太低然後跳回),我將prevOffset設置爲MAX( 0,contentSize.height-frame.size.height)。
這些東西並不是必需的,但你得到的是滾動/滾動的Scroll/TableViews,試用它們。
完美謝謝@Jsdodgers – MrBeanzy 2013-03-19 23:43:41