Ios 自动布局的Scrollview键盘问题
我的屏幕上有多个Ios 自动布局的Scrollview键盘问题,ios,objective-c,ios6,uiscrollview,autolayout,Ios,Objective C,Ios6,Uiscrollview,Autolayout,我的屏幕上有多个文本字段。因此,主视图具有scrollview,边约束设置为0Scrollview有一个子视图正好适合Scrollview。现在这个子视图有了所有的文本字段 问题是当用户按下键盘上方工具栏上的“上一个/下一个”按钮时,在文本字段之间导航时。问题仅出现在iOS 6中 按下Next按钮时,它工作正常。对于Previous按钮,它适用于所有TextFields,但位于所有TextFields第二行的除外。那么,当第五个文本字段处于活动状态时,我继续按上一个转到第二个文本字段,会发生什么
文本字段
。因此,主视图具有scrollview
,边约束设置为0Scrollview
有一个子视图正好适合Scrollview
。现在这个子视图有了所有的文本字段
问题是当用户按下键盘上方工具栏上的“上一个/下一个”按钮时,在文本字段之间导航时。问题仅出现在iOS 6中
按下Next
按钮时,它工作正常。对于Previous
按钮,它适用于所有TextFields
,但位于所有TextFields
第二行的除外。那么,当第五个文本字段
处于活动状态时,我继续按上一个
转到第二个文本字段
,会发生什么情况呢,如果TextField
位于NavigationBar
后面或更上方,则应更改scrollview的contentOffset
,以便TextField
向下显示。但事实上,它的上升幅度更大。此外,底部的按钮应放置在superview底部的20pts
,通常情况下是这样。但当出现此问题时,按钮在superview底部的可见度超过了20pts
。在这里,当我滚动一点点的时候,scrollview的内容就会急剧向下移动,并到达正确的位置。按钮现在在正确的位置
第一个图像的位置不正确。第二张图片的按钮位置正确
文本字段之间导航的代码:字段的标记值为1到5
// 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];
}
}];
}
你对这里的问题有什么想法吗?我已经发布了很多代码,但是我问我你是否需要更多的代码 我努力寻找一个导致问题的方法,即使我在每个方法中设置了断点,也没有运气。不知道我是否错过了一个或一些神奇的东西
但最后一个问题是检查textfield是否隐藏在导航栏后面或上面的代码。我纠正了它,现在它的工作
在我的例子中,下面是检查它的正确方法。它可能和你的不同
if (self.selectedTextField.frame.origin.y < self.scrollView.contentOffset.y)
{
// TextField is hidden behind navigation bar
}
if(self.selectedTextField.frame.origin.y
if (self.selectedTextField.frame.origin.y < self.scrollView.contentOffset.y)
{
// TextField is hidden behind navigation bar
}