How to adjust the view position when the keyboard

2019-06-01 03:02发布

问题:

In forms that are longer than 50% of the screen area, inputs will appear underneath the keyboard. I want to adjust the view to keep the selected input always visible when the keyboard shows and reset its position when the keyboard disappears.

Any ideas? Thanks.

回答1:

The section Managing the Keyboard in the iPhone Application Programming Guide discusses how to receive notifications of keyboard display/undisplay, as well as how to keep your content visible (including sample code).



回答2:

If you're in a UIScrollView, you can use the -scrollRectToVisible:animated: method to move to a particular portion of the view.

If you're in a UITableView, you can use the -scrollToRowAtIndexPath:atScrollPosition:animated: method to scroll to a particular row, or - scrollToNearestSelectedRowAtScrollPosition:animated:.



回答3:

Use https://github.com/michaeltyson/TPKeyboardAvoiding to manage Keyboards automatically



回答4:

Try This Code:

#define kTextFieldMovementDistance      150.0 //You can set this according to your need
#define kMinimumMovementDuration        0.3f

- (void) slidingViewUpward: (BOOL) isSlidingUp
{
    CGFloat movementDistance = kTextFieldMovementDistance; 

    const float movementDuration = kMinimumMovementDuration;

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

    [UIView beginAnimations: @"animation" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);

    [UIView commitAnimations];
}

When you want to slide the view in upwards Direction, or when the Keyboard became the first responder, the pass the bool value as YES otherwise NO.