Iphone 显示键盘时将UITextField滚动到视图中

Iphone 显示键盘时将UITextField滚动到视图中,iphone,scroll,uitextfield,iphone-softkeyboard,Iphone,Scroll,Uitextfield,Iphone Softkeyboard,我正在编写一个针对iOS 6.1的应用程序。我正试着按照下面的说明,如果UITextField被键盘遮挡,如何将其滚动到视图中。问题是,苹果公司记录在案的计算滚动点的算法并不能很好地工作。我的计算滚动点的算法只稍微好一点,但却差了70像素。计算滚动点的正确方法是什么 下面您可以看到,从左到右,显示键盘前的我的视图,使用Apple算法计算滚动点的滚动后的我的视图,以及使用我的算法计算滚动点的滚动后的我的视图。(该网格中的每个正方形为25像素乘以25像素。) 这是我正在使用的代码。注意#if AP

我正在编写一个针对iOS 6.1的应用程序。我正试着按照下面的说明,如果UITextField被键盘遮挡,如何将其滚动到视图中。问题是,苹果公司记录在案的计算滚动点的算法并不能很好地工作。我的计算滚动点的算法只稍微好一点,但却差了70像素。计算滚动点的正确方法是什么

下面您可以看到,从左到右,显示键盘前的我的视图,使用Apple算法计算滚动点的滚动后的我的视图,以及使用我的算法计算滚动点的滚动后的我的视图。(该网格中的每个正方形为25像素乘以25像素。)

这是我正在使用的代码。注意
#if APPLE\u WAY

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.view.contentInset = contentInsets;
    self.view.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {

#if APPLE_WAY
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
#else
        CGFloat offset = self.activeField.frame.origin.y;

        //TODO: Why is this off by 70?
        offset = offset - 70;

        CGPoint scrollPoint = CGPointMake(0.0, offset);
#endif
        [self.view setContentOffset:scrollPoint animated:YES];
    }
}

您只需将偏移设置为键盘的高度:

CGFloat offset = kbSize.height;
CGPoint scrollPoint = CGPointMake(0.0, offset);
[self.view setContentOffset:scrollPoint animated:YES];

希望这有帮助

您只需将偏移设置为键盘的高度:

CGFloat offset = kbSize.height;
CGPoint scrollPoint = CGPointMake(0.0, offset);
[self.view setContentOffset:scrollPoint animated:YES];

希望这有帮助

有几种方法可以做到这一点。考虑到你的情况,我会这样做:

- (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0, 0);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}
然后,在viewDidLoad方法中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWasShown:)
                                         name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

有几种方法可以做到这一点。考虑到你的情况,我会这样做:

- (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0, 0);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}
然后,在viewDidLoad方法中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWasShown:)
                                         name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

当你有一个滚动视图时,这很容易,因为你甚至不需要滚动;您所要做的就是调整contentInset和ScrollIndicationSet以避开键盘,其余的操作将自动进行:

- (void) keyboardShow: (NSNotification*) n {
    self->_oldContentInset = self.scrollView.contentInset;
    self->_oldIndicatorInset = self.scrollView.scrollIndicatorInsets;
    self->_oldOffset = self.scrollView.contentOffset;
    NSDictionary* d = [n userInfo];
    CGRect r = [[d objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    r = [self.scrollView convertRect:r fromView:nil];
    CGRect f = self.fr.frame;
    CGFloat y =
        CGRectGetMaxY(f) + r.size.height -
            self.scrollView.bounds.size.height + 5;
    if (r.origin.y < CGRectGetMaxY(f))
        [self.scrollView setContentOffset:CGPointMake(0, y) animated:YES];
    UIEdgeInsets insets;
    insets = self.scrollView.contentInset;
    insets.bottom = r.size.height;
    self.scrollView.contentInset = insets;
    insets = self.scrollView.scrollIndicatorInsets;
    insets.bottom = r.size.height;
    self.scrollView.scrollIndicatorInsets = insets;
}
-(无效)键盘显示:(NSNotification*)n{
self->\u oldContentInset=self.scrollView.contentInset;
self->\u oldIndicationSet=self.scrollView.ScrollIndicationSet;
self->\u oldOffset=self.scrollView.contentOffset;
NSDictionary*d=[n用户信息];
CGRect r=[[d objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
r=[self.scrollView convertRect:r fromView:nil];
CGRect f=self.fr.frame;
CGY=
CGRectGetMaxY(f)+r.size.height-
self.scrollView.bounds.size.height+5;
if(r.origin.y
我在书的这一部分给出了一系列不同的策略和代码:


所有这些都得到了可下载项目的支持,您可以在我的github网站上获取这些项目

当你拥有一个滚动视图时,这很容易,因为你甚至不需要滚动;您所要做的就是调整contentInset和ScrollIndicationSet以避开键盘,其余的操作将自动进行:

- (void) keyboardShow: (NSNotification*) n {
    self->_oldContentInset = self.scrollView.contentInset;
    self->_oldIndicatorInset = self.scrollView.scrollIndicatorInsets;
    self->_oldOffset = self.scrollView.contentOffset;
    NSDictionary* d = [n userInfo];
    CGRect r = [[d objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    r = [self.scrollView convertRect:r fromView:nil];
    CGRect f = self.fr.frame;
    CGFloat y =
        CGRectGetMaxY(f) + r.size.height -
            self.scrollView.bounds.size.height + 5;
    if (r.origin.y < CGRectGetMaxY(f))
        [self.scrollView setContentOffset:CGPointMake(0, y) animated:YES];
    UIEdgeInsets insets;
    insets = self.scrollView.contentInset;
    insets.bottom = r.size.height;
    self.scrollView.contentInset = insets;
    insets = self.scrollView.scrollIndicatorInsets;
    insets.bottom = r.size.height;
    self.scrollView.scrollIndicatorInsets = insets;
}
-(无效)键盘显示:(NSNotification*)n{
self->\u oldContentInset=self.scrollView.contentInset;
self->\u oldIndicationSet=self.scrollView.ScrollIndicationSet;
self->\u oldOffset=self.scrollView.contentOffset;
NSDictionary*d=[n用户信息];
CGRect r=[[d objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
r=[self.scrollView convertRect:r fromView:nil];
CGRect f=self.fr.frame;
CGY=
CGRectGetMaxY(f)+r.size.height-
self.scrollView.bounds.size.height+5;
if(r.origin.y
我在书的这一部分给出了一系列不同的策略和代码:


所有这些都得到了可下载项目的支持,您可以在我的github网站上获取这些项目

那代码正确吗?首先您设置了
offset=self.activeField.frame.origin.y
,然后用
offset=kbSize.height
覆盖该值。很抱歉,我复制时忘记了:)编辑了答案代码正确吗?首先设置
offset=self.activeField.frame.origin.y
,然后用
offset=kbSize.height
覆盖该值,很抱歉在复制时忘记了:)编辑答案您的解决方案使用
CGPoint scrollPoint=CGPointMake(0.0,self.activeField.frame.origin.y-kbSize.height)与苹果的算法相同。也许我错过了;您的解决方案如何解决此问题?您的解决方案使用
CGPoint scrollPoint=CGPointMake(0.0,self.activeField.frame.origin.y-kbSize.height)与苹果的算法相同。也许我错过了;你的解决方案如何解决这个问题?