How to adjust the view position when the keyboard opens in iPhone?
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 a开发者_JS百科nd reset its position when the keyboard disappears.
Any ideas? Thanks.
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).
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:
.
Use https://github.com/michaeltyson/TPKeyboardAvoiding to manage Keyboards automatically
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
.
精彩评论