Xcode/iOS5:键盘出现时向上移动UIView

fil*_*lou 49 iphone keyboard uiviewcontroller ios ios5

当键盘显示时,我想提升视野.键盘(高度:216)应该用它的高度向上推动我的视线.这可以通过简单的代码实现吗?

djr*_*ero 97

要移动视图up,只需更改它center.首先,将原始版本保留在CGPoint属性中.

- (void)viewDidLoad 
{
    ...
    self.originalCenter = self.view.center;
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,在键盘显示时根据需要进行更改:

self.view.center = CGPointMake(self.originalCenter.x, /* new calculated y */);
Run Code Online (Sandbox Code Playgroud)

最后,在隐藏键盘时恢复它:

self.view.center = self.originalCenter;
Run Code Online (Sandbox Code Playgroud)

根据需要添加动画糖

您有多种方法可以知道键盘出现的时间.

观察UIKeyboardDidShowNotification通知.

/* register notification in any of your initWithNibName:bundle:, viewDidLoad, awakeFromNib, etc. */
{
    ...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];     
    ...
}

- (void)keyboardDidShow:(NSNotification *)note 
{
    /* move your views here */
}
Run Code Online (Sandbox Code Playgroud)

与之相反UIKeyboardDidHideNotification.

-要么-

实现UITextFieldDelegate

在编辑开始/结束时检测以移动视图.

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    /* keyboard is visible, move views */
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    /* resign first responder, hide keyboard, move views */
}
Run Code Online (Sandbox Code Playgroud)

根据您可能需要跟踪用户编辑的字段的实际文本字段,添加计时器以避免过多地移动视图.

  • 我只是试图将此解决方案用于类似的问题.此解决方案存在一个问题:您应该在viewDidAppear而不是viewDidLoad中设置originalCenter参数.这是因为UI元素在循环中的此时尚未放置在它们的位置,而是由viewDidAppear放置.viewDidLoad中的originalCenter给了我x = 0,y = 0. (6认同)
  • 谢谢madmw,这很完美:) (2认同)

Ten*_*kar 31

这样做.键盘可见后使用此代码

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.frame = CGRectMake(0,-10,320,480);
    [UIView commitAnimations];

}
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 13

我以与djromero类似的方式做到了这一点,除了我调整了视图的帧原点而不是它的中心.

我正在移动的视图是UIScrollView,我希望它相对于UITextField元素移动,以便始终显示文本字段.此文本字段的位置可能会根据滚动视图的偏移量而有所不同.

所以我的代码看起来像这样:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.scrollView.frame = CGRectMake(0,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height);
        [UIView commitAnimations];
    });
    return YES;
}

- (NSInteger)getKeyBoardHeight:(NSNotification *)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    NSInteger keyboardHeight = keyboardFrameBeginRect.size.height;
    return keyboardHeight;
}

-(void) keyboardDidShow:(NSNotification*) notification
{
    NSInteger keyboardHeight;
    keyboardHeight = [self getKeyBoardHeight:notification];
    NSInteger scrollViewFrameHeight = self.scrollView.frame.size.height;
    NSInteger textFieldRelativePosition = self.tableView.frame.origin.y - self.scrollView.contentOffset.y;
    NSInteger textFieldFrameOffset = scrollViewFrameHeight - textFieldRelativePosition;
    NSInteger movement = MAX(0,keyboardHeight-textFieldFrameOffset); // Offset from where the keyboard will appear.
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.scrollView.frame = CGRectMake(0,-movement,
                                           self.scrollView.frame.size.width,
                                           self.scrollView.frame.size.height);
        [UIView commitAnimations];
    });
}
Run Code Online (Sandbox Code Playgroud)

视图控制器是一个UITextFieldDelegate,并且还订阅了UIKeyboardDidShowNotification,以便我们能够访问键盘的大小.

当键盘显示时,我们计算UITextField(调整滚动偏移量)和键盘的相对偏移量,它们会改变UIScrollView的原点,使其移动到足以让UITextField显示.

如果即使出现键盘,UITextField仍会显示,则原点不会改变.

  • +1表示如何获得键盘高度 (4认同)

Kis*_*har 13

这是实现此目的最简单有效的方法:

添加以下常量:

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 = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;    
Run Code Online (Sandbox Code Playgroud)

将其添加到视图控制器:

CGFloat animatedDistance;
Run Code Online (Sandbox Code Playgroud)

并将这些方法添加到您的代码中:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

  • 非常适合我,谢谢你,我把它放进去,设置我的UITextField委托,然后完成剩下的工作.谢谢! (2认同)