iOS scrollView setContentOffset“shimmy”

时间:2011-12-22 20:45:33

标签: ios uitextfielddelegate

我有一个带有多个textFields的scrollView,它跟踪活动字段并确保在弹出键盘时它是可见的。这一切都运行良好,但是当我从第3个到第4个textField选项卡时,我会在textField最终出现在正确位置之前上下一点“shimmy”。有什么建议吗?

-(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];
    }    
}

1 个答案:

答案 0 :(得分:0)

您在哪里设置keyboardIsShown?你检查它是否已经设置后,你不想这样做吗?

然后:是滚动视图末尾附近的第4个字段,你有弹跳滚动设置吗?