键盘弹出后如何将聚焦文本字段移动到视图中间

时间:2013-02-11 09:32:14

标签: iphone objective-c uitableview uiscrollview custom-cell

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

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

所以即时使用 TPKeyboardAvoidingScrollView 框架,但它不适用于 ios 5.0+.

有时无法滚动桌面上的滚动视图。

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

我应该使用什么?

先谢谢。

3 个答案:

答案 0 :(得分: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()之前添加此静态const您可以根据需要更改上面的键盘高度值。然后添加以下代码:

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

答案 1 :(得分: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];
}

希望这有帮助!

答案 2 :(得分:0)

首先打开xib文件并将内容插入(在Scrollview大小中)设置为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];
}