我设法做到这一点的最好方法是抓住键盘框架,然后在文本视图获取 textViewDidBeginEditing: 调用时更新我的滚动视图插图。这里我使用的是表格视图,但相同的逻辑应该适用于滚动视图,主要区别在于滚动方式。我使用 scrollToRowAtIndexPath,你会想使用 scrollRectToVisible
//setup keyboard callbacks
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
//this is called from your UITextViewDelegate when textViewDidBeginEditing: is called
- (void)updateActiveTextScroll:(UITextView*)textView
{
activeTextView = textView;
UIEdgeInsets inset;
UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
if( UIInterfaceOrientationIsLandscape(orient) )
{
inset = UIEdgeInsetsMake(0.0, 0.0, kbFrame.size.width, 0.0);
}
else
{
inset = UIEdgeInsetsMake(0.0, 0.0, kbFrame.size.height, 0.0);
}
myTableView.contentInset = inset;
myTableView.scrollIndicatorInsets = inset;
[myTableView scrollToRowAtIndexPath:activeNSIndexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
//dont forget to reset when the keyboard goes away
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets inset = UIEdgeInsetsZero;
myTableView.contentInset = inset;
myTableView.scrollIndicatorInsets = inset;
}