首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS scrollView setContentOffset "shimmy“

iOS scrollView setContentOffset "shimmy“
EN

Stack Overflow用户
提问于 2011-12-23 04:45:33
回答 1查看 447关注 0票数 0

我有一个带有多个textFields的scrollView,它跟踪活动字段,并确保当键盘弹出时它是可见的。这一切都运行得很好,但是当我从第三个textField切换到第四个textField时,我会在Tab键到达正确的位置之前感觉到一点上下的“摆动”。有什么建议吗?

代码语言:javascript
复制
-(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];
    }    
}
EN

回答 1

Stack Overflow用户

发布于 2011-12-23 05:33:33

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8609532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档