我有多个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。按钮现在处于正确的位置。
第一张图片的位置不正确。第二张图像具有正确的按钮位置。
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}}
关于这里可能出现什么问题的任何想法?我已经发布了很多代码,但是我问你是否需要更多代码。
答案 0 :(得分:0)
我努力寻找一种导致问题并且没有运气的方法,即使我在每种方法中都设置了断点。不知道我是否错过了一个或一个神奇的东西。
但最后问题是在检查文本字段是隐藏在导航栏后面还是上方的代码中。我纠正了它,现在它起作用了。
在我的情况下,下面是检查它的正确方法。它可能与你的不同。
if (self.selectedTextField.frame.origin.y < self.scrollView.contentOffset.y)
{
// TextField is hidden behind navigation bar
}