简体   繁体   English

iOS的scrollView setContentOffset“ Shimmy”

[英]iOS scrollView setContentOffset “shimmy”

I have a scrollView with multiple textFields, which tracks the active field and makes sure it is visible when the keyboard pops up. 我有一个带有多个textFields的scrollView,它可以跟踪活动字段并确保在弹出键盘时它是可见的。 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. 一切都很好,但是当我从第3到第4文本字段制表符时,在textField出现在正确的位置之前,我会有点上下晃动。 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];
    }    
}

Where do you set keyboardIsShown ? 您在哪里设置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? 然后:滚动视图结尾处的第四个字段是否已出现并设置了滚动滚动集?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM