键盘消失后,iOS视图自动向下移动 [英] iOS view move down automatically after keyboard disappear

查看:176
本文介绍了键盘消失后,iOS视图自动向下移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad项目,在它的一个视图控制器中,一个面板视图中有两个TextField(视图是在Storyboard中构建的).我想实现的是,当这些文本字段中的任何一个成为第一响应者(即出现键盘)时,面板视图将向上移动,如果键盘消失,它将向下移动至原点位置.

I had a iPad project, inside one of its view controllers, there are two TextFields inside a panel view (Views are build in Storyboard). What I would like to achieve is when any of those textfield become first responder (i.e. Keyboard Appears), the panel view will move up and if keyboard disappear it will move down to origin position.

一旦测试向上移动,我发现在键盘消失后视图将自动移回原点位置,但是我没有编写任何代码来做到这一点.

Once I test move up, I found the view will automatically move back to origin position after keyboard disappear, but I didn't write any code to do that.

要向上移动的代码:

- (void)moveUpTextFields {
    if ([self.emailTextField isFirstResponder] || [self.passwordTextField isFirstResponder]) {     
        CGRect frame = self.textFieldPanel.frame;
        frame.origin.y -= 50;
        self.textFieldPanel.frame = frame;
    }
}

所以我想弄清楚这是怎么发生的,以及我应该如何实现我的目标(即,如果出现键盘,则向上移动并回到原始位置)?

So I would like to figure out how does that happen, and how should I achieve my goal (i.e. Move up if keyboard appear and back to original position)?

更新:

要实现我的目标,它应该使用键盘通知.在viewWillAppear

To achieve my goals, it should use Keyboard Notification. Register these notifications in viewWillAppear

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

很久以前,我在另一个项目中做了这个,只是一开始不记得了:(

I did this in another project long time ago, just didn't remember it at the first place :(

推荐答案

好,所以奇怪的行为是因为当键盘出现或消失时,它将中继视图控制器的子视图并触发-(void)viewDidLayoutSubviews并将视图重新定位到StoryBoard的位置.如果视图是通过代码创建的,那么这将不是问题.

OK, so the weird behaviour is because when Keyboard Appear or Disappear, it will relayout view controller's sub views and trigger -(void)viewDidLayoutSubviews and reposition view back to StoryBoard's position. If the view is created by code then this won't be a issue.

除了为KeyBoard添加通知外,我们还需要在viewDidLayoutSubviews中重新定位视图,因为通知是在布局子视图之前触发的.

In addition to add notifications for KeyBoard, we also need to reposition the view in viewDidLayoutSubviews since the notification triggered before layout subviews.

- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGFloat y = self.view.frame.size.height - keyboardSize.height - self.textFieldPanel.frame.size.height - 5;

    CGRect frame = self.textFieldPanel.frame;
    panelViewYOffset = frame.origin.y - y;
    frame.origin.y -= panelViewYOffset;

    // Since it will layout subviews in viewDidLayout, why we set frame here?
    // I tried to move this line, turns out the panel will move up but the animation is gone
    // So this line will have a nice liner animation curve but I don't why, may be someone can explain it.
    self.textFieldPanel.frame = frame;    
}

- (void)keyboardWillHide:(NSNotification *)notification {
    CGRect frame = self.textFieldPanel.frame;
    frame.origin.y += panelViewYOffset;
    self.textFieldPanel.frame = frame;

    panelViewYOffset = 0;
}

在viewDidLayoutSubviews中:

In viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews {
    CGRect frame = self.textFieldPanel.frame;
    frame.origin.y -= panelViewYOffset;

    self.textFieldPanel.frame = frame;
}

就像我提到的那样,该方法仅适用于StoryBoard(界面生成器)中的视图,如果self.textFieldPanel是通过代码创建的,则应删除viewDidLayoutSubviews中的代码.

Like I mentioned, this approach only works for view from StoryBoard (Interface Builder), if self.textFieldPanel is created by code, then the code in viewDidLayoutSubviews should be removed.

这篇关于键盘消失后,iOS视图自动向下移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆