iOS的scrollView setContentOffset“shimmy”

发布于 2024-12-22 08:25:34 字数 1899 浏览 3 评论 0原文

我有一个带有多个文本字段的滚动视图,它跟踪活动字段并确保它在键盘弹出时可见。一切都运行良好,但是当我从第三个文本字段切换到第四个文本字段时,在文本字段最终到达正确位置之前,我会出现一点上下“晃动”。有什么建议吗?

-(void)keyboardDidShow:(NSNotification *)notification
{    
    if (keyboardIsShown)return;

    NSDictionary* info=[notification userInfo];
    // get keyboard size
    CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
    //Set scrollview insets to make room for keyboard
    UIEdgeInsets contentInsets=UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    scrollView.contentInset=contentInsets;
    scrollView.scrollIndicatorInsets=contentInsets;

    //scroll the active text field into view
    CGRect viewFrame=self.view.frame;
    viewFrame.size.height-=keyboardSize.height;
    int fieldHeight=self.currentTextField.bounds.size.height;
    CGFloat navHeight=self.navigationController.navigationBar.frame.size.height;
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);

    if (!CGRectContainsPoint(viewFrame, viewPoint)) {
        //scroll to make sure active field is showing
        CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-keyboardSize.height+navHeight);//+navHeight
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

-(void)showActiveField
{    
    //this makes sure that activeField shows when selecting another field after initial keyboard show
    int fieldHeight=self.currentTextField.bounds.size.height;
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
    CGRect viewFrame=self.view.frame;

    int inset=scrollView.contentInset.bottom;
    if (!CGRectContainsPoint(viewFrame, viewPoint)) {
        //scroll to make sure active field is showing
        CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-inset);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }    
}

I have a scrollView with multiple textFields, which tracks the active field and makes sure it is visible when the keyboard pops up. It all works well, but when I tab from the 3rd to 4th textField, I get a little up and down "shimmy" before the textField ends up in the right place. Any suggestions?

-(void)keyboardDidShow:(NSNotification *)notification
{    
    if (keyboardIsShown)return;

    NSDictionary* info=[notification userInfo];
    // get keyboard size
    CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
    //Set scrollview insets to make room for keyboard
    UIEdgeInsets contentInsets=UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    scrollView.contentInset=contentInsets;
    scrollView.scrollIndicatorInsets=contentInsets;

    //scroll the active text field into view
    CGRect viewFrame=self.view.frame;
    viewFrame.size.height-=keyboardSize.height;
    int fieldHeight=self.currentTextField.bounds.size.height;
    CGFloat navHeight=self.navigationController.navigationBar.frame.size.height;
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);

    if (!CGRectContainsPoint(viewFrame, viewPoint)) {
        //scroll to make sure active field is showing
        CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-keyboardSize.height+navHeight);//+navHeight
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

-(void)showActiveField
{    
    //this makes sure that activeField shows when selecting another field after initial keyboard show
    int fieldHeight=self.currentTextField.bounds.size.height;
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
    CGRect viewFrame=self.view.frame;

    int inset=scrollView.contentInset.bottom;
    if (!CGRectContainsPoint(viewFrame, viewPoint)) {
        //scroll to make sure active field is showing
        CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-inset);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-12-29 08:25:34

你在哪里设置keyboardIsShown?您不想在检查它是否已经设置后立即执行此操作吗?

然后:第四个字段是否靠近滚动视图的末尾并且您设置了反弹滚动?

Where do you set keyboardIsShown? Don't you want to do that Right after you check if it is already set?

And then: is the 4th field near the end of the scrollview and you have bounce scroll set?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文