0

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

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

1 回答 1

0

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

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

于 2011-12-22T21:33:33.463 回答