Scrollview-autolayout的键盘问题

时间:2014-05-21 13:28:21

标签: ios objective-c ios6 uiscrollview autolayout

我有多个TextFields的屏幕。因此,主视图的scrollview限制边缘设置为0. Scrollview有一个完全符合scrollview的子视图。现在这个子视图已全部TextFields

问题是在用户按下键盘上方工具栏上的TextFields按钮时在Previous/Next之间导航。问题仅在iOS 6

按下Next按钮时工作正常。对于Previous按钮,它适用于所有TextFields,除了位于所有TextFields的第二行中的一个TextField。那么当第五个Previous处于有效状态时会发生什么,如果TextField落后于TextField甚至高于NavigationBar,我会一直按scrollview's转到第二个contentOffset然后应更改TextField 20pts,以便20pts降低并变为可见。但实际上它更具有上升空间。底部的按钮也应该从其超视图的底部放置// Called when previous/next button is pressed - (void)navigateBetweenTextField:(UISegmentedControl *)segmentedControl { // Check which segment is selected and change textfield accordingly UITextField * textFieldToMakeActive = nil; // If Previous is pressed if (segmentedControl.selectedSegmentIndex == 0) { textFieldToMakeActive = (UITextField *)[self.view viewWithTag:(self.selectedTextField.tag - 1)]; } // Else if Next is pressed else if (segmentedControl.selectedSegmentIndex == 1) { textFieldToMakeActive = (UITextField *)[self.view viewWithTag:(self.selectedTextField.tag + 1)]; } segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment; // If textfield is not nil if (textFieldToMakeActive) { // Make it active [textFieldToMakeActive becomeFirstResponder]; } } ,这通常就是这种情况。但是当出现此问题时,按钮在其超级视图的底部可见// Called when the UIKeyboardWillShowNotification is sent. - (void)keyboardWillShow:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; NSTimeInterval animationDuration; [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; self.keyboardAndAccessoryViewSize = kbSize; [self moveViewSoThatTextFieldIsVisible:kbSize]; } // It checks if textfield is visible and then moves the view to make it visible if not. - (void)moveViewSoThatTextFieldIsVisible:(CGSize)kbSize { // Change the contentInset UIEdgeInsets contentInsets = self.scrollView.contentInset; // If iOS 7 or above then scrollview ends on bottom of screen. Thus, bottom of content inset should be moved by keyboard's height. if ([self iOS7OrAbove]) { contentInsets.bottom = kbSize.height; } // Else scrollview ends above tabbar at bottom. Thus, bottom of content inset that should be moved is only by keyboard's height - tabbar's height else { contentInsets.bottom = kbSize.height - self.tabBarController.tabBar.bounds.size.height; DLog(@"Content inset bottom : %f", contentInsets.bottom); } [UIView animateWithDuration:0.25 animations:^{ self.scrollView.contentInset = contentInsets; self.scrollView.scrollIndicatorInsets = contentInsets; CGFloat textFieldDistanceFromOrigin = self.selectedTextField.frame.origin.y - self.scrollView.contentOffset.y + self.selectedTextField.frame.size.height + TEXTFIELD_MARGIN; CGFloat visibleViewHeightAfterKeyboardDisplay = self.scrollView.bounds.size.height - kbSize.height + TOOLBAR_HEIGHT; // If active text field is hidden by keyboard, scroll it so it's visible if(textFieldDistanceFromOrigin > visibleViewHeightAfterKeyboardDisplay) { DLog(@"Textfield is hidden by keyboard."); // Calculate offset y value to add to scrollview's contentOffset so that textfield is visible then. CGFloat offsetToAdd = textFieldDistanceFromOrigin - visibleViewHeightAfterKeyboardDisplay; [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y + offsetToAdd) animated:YES]; } if (textFieldDistanceFromOrigin < self.selectedTextField.bounds.size.height) { DLog(@"Textfield is above visible screen area."); [self.scrollView setContentOffset:CGPointMake(0, self.selectedTextField.frame.origin.y - TEXTFIELD_MARGIN) animated:YES]; } }]; } 以上。在这里,当我滚动一点点然后滚动视图内容急剧下移并到达正确的位置.i.e。按钮现在处于正确的位置。

第一张图片的位置不正确。第二张图像具有正确的按钮位置。

enter image description here

enter image description here

TextFields之间的导航代码:字段的标记值为1到5。

// This is called when TextField becomes active
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Store TextField which has just become active
    self.selectedTextField = textField;
    [self moveViewSoThatTextFieldIsVisible:self.keyboardAndAccessoryViewSize];
}

键盘即将可见时调用的方法:

{{1}}

TextField委托方法:

{{1}}

关于这里可能出现什么问题的任何想法?我已经发布了很多代码,但是我问你是否需要更多代码。

1 个答案:

答案 0 :(得分:0)

我努力寻找一种导致问题并且没有运气的方法,即使我在每种方法中都设置了断点。不知道我是否错过了一个或一个神奇的东西。

但最后问题是在检查文本字段是隐藏在导航栏后面还是上方的代码中。我纠正了它,现在它起作用了。

在我的情况下,下面是检查它的正确方法。它可能与你的不同。

if (self.selectedTextField.frame.origin.y < self.scrollView.contentOffset.y)
{
    // TextField is hidden behind navigation bar
}