我在 iPad 应用程序中滚动屏幕时遇到问题。在我的应用程序中,页面底部附近可能有很多字段。因此,我不想根据当前活动字段的位置频繁调整屏幕位置,而是希望屏幕处于以下两个位置之一:滚动或不滚动。如果用户正在处理任何部分将被键盘隐藏的字段,我想将屏幕向上滚动键盘的高度。
我的问题是,在键盘出现并且屏幕滚动了我的代码指定的距离后,屏幕会迅速回落到一个位置,使得光标(无论它在哪里)正好位于键盘顶部的上方。我不明白是什么导致了最后的快照。我的滚动代码(从教程中提取)如下。
谢谢
//Scroll the screen up if the keyboard hides the current field
- (void)keyboardDidShow:(NSNotification *)notification
{
// Step 1: Get the height of the keyboard.
if ([self interfaceOrientation] == UIInterfaceOrientationPortrait || [self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
}
else
{
keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.width;
}
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
[(UIScrollView*)[self view] setContentInset: contentInsets];
[(UIScrollView*)[self view] setScrollIndicatorInsets: contentInsets];
// Step 3: Scroll the screen up by the height of the keyboard.
visibleRect = self.view.frame;
visibleRect.size.height -= (keyboardHeight + [[self toolbar] bounds].size.height);
if (([self.activeField frame].origin.y + [self.activeField frame].size.height) > visibleRect.size.height)
{
//make sure scrolling is vertical only
CGPoint scrollPoint = CGPointMake(currentLeftEdge, keyboardHeight);
[(UIScrollView*)[self view] setContentOffset: scrollPoint animated:YES];
}
}