簡體   English   中英

鍵盤彈出后如何將聚焦的文本字段移動到視圖的中間

[英]How to move the focussed text field to the middle of the view after keyboard popups

在我的iPad中,我創建了SplitView應用程序。 我有一個包含多行UITableVewtable ,每行/單元格具有三個UITextField

當我點擊UITextField某些字段會被鍵盤隱藏。

所以即時通訊使用TPKeyboardAvoidingScrollView框架,但它不適用於ios 5.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];
}

您想要的是對視圖進行動畫處理,例如,當您觸摸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];
}

希望這可以幫助 !

首先打開xib文件,並將Content insets(在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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM