How to adjust the view position when the keyboard opens in iPhone?

孤者浪人 提交于 2019-12-10 11:39:56

问题


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.



来源:https://stackoverflow.com/questions/1593685/how-to-adjust-the-view-position-when-the-keyboard-opens-in-iphone

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