0

在我的 iPad 即时创建SplitView应用程序。我有一个UITableVewtable行,每行/单元格有三个UITextField

当我点击UITextField某些字段时,键盘会隐藏。

所以我使用TPKeyboardAvoidingScrollView框架,但它不起作用ios 5.0+.

有时无法滚动表格上的滚动视图。

所以我想每次都将焦点单元移动到键盘上方的中间/正上方

我应该使用什么?

提前致谢。

4

3 回答 3

0

首先打开 xib 文件并将 Content insets(In Scrollview Size) 设置为 200

现在在 .m 文件中更新以下两种方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell=@"Cell";        
    Cell *customCell= (Cell *)[tableView dequeueReusableCellWithIdentifier:cell];

    if (customCell==nil) {
        NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        for (id object in bundle) {
            if ([object isKindOfClass:[Cell class]])
            {
                customCell = (Cell *)object;
                break;
            }
        }
    }
    customCell.IBTxtfield.tag=indexPath.row;
return customCell;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSIndexPath *path=[NSIndexPath indexPathForRow:textField.tag inSection:0];
    [IBtblView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
于 2013-02-12T08:57:45.323 回答
0

试试这个代码。把它放在你的 .h 文件中:

CGFloat animatedDistance;

在您的 .m 文件中:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352;

在 @interface myclass () 之前添加此静态常量您可以根据需要更改上面的键盘高度值。然后添加以下代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    CGRect textFieldRect =
    [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect =
    [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;

    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size. height;

    CGFloat denominator =
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;

    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}
于 2013-02-11T09:41:43.143 回答
0

您想要的是为您的视图设置动画,例如当您触摸 textField 时它应该向上,当您结束输入时它应该向下。您可以通过实现两个 UITextFiled 委托方法和一些小动画来实现这一点,如下所示:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self startAnimatingTextField: textField up: NO];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self startAnimatingTextField: textField up: YES];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];

    [UIView commitAnimations];
}

-(void)startAnimatingTextField:(UITextField *) textField up: (BOOL) up
{
    const int distance = 120;   /* modify conform your needs */
    const float duration = 0.3f;                        

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];

    self.view.frame = CGRectOffset(self.view.frame, 0, movement);

    [UIView commitAnimations];
}

希望这可以帮助 !

于 2013-02-11T09:52:08.847 回答