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

我怕爱的太早我们不能终老 提交于 2020-01-26 04:05:06

问题


In my iPad im creating SplitView application. I have a UITableVewtable with multiple rows, each row/cell having three UITextField

When i tap on UITextField some fields are hide by the keyboard.

So im using TPKeyboardAvoidingScrollView framework but it does not works on ios 5.0+.

some times unable to scroll the scroll view on the table.

So I want to move the focussed cell to the middle/ just above the key board at every time

What should I use?

Thanks in Advance.


回答1:


Try this code. Put this in your .h file :

CGFloat animatedDistance;

In your .m file :

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;

Add this static const before your @interface myclass () You can change the keyboard height values above as per your need. And then add the following code :

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



回答2:


what you want is to animate your view such as when you touch the textField it should go up and when you end typing it should go down. You can achieve this by implementing two UITextFiled delegate methods and some little animation as follows :

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

Hope this helps !




回答3:


First open the xib file and set the Content insets(In Scrollview Size) to 200

Now update following two methods as below in .m file.

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


来源:https://stackoverflow.com/questions/14809290/how-to-move-the-focussed-text-field-to-the-middle-of-the-view-after-keyboard-pop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!