UITextField键盘激活时移动视图

时间:2013-08-01 00:16:59

标签: ios objective-c uitextfield

当我激活UITextField时会出现以下问题(如果我只显示几张图片,请尝试以UIView which is nested within my UITableViewController作为页脚。

我可以删除这个问题。 Jeez

3 个答案:

答案 0 :(得分:1)

据我所知,当UITextField成为第一个响应者时(即将键盘添加到视图中),您试图移动整个视图?如果是这种情况,我会在UITextField委托方法中添加代码:

#define VIEW_TAG 12345
#define kKeyboardOffsetY 80.0f

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    // get a reference to the view you want to move when editing begins
    // which can be done by setting the tag of the container view to VIEW_TAG
    UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
    [UIView animateWithDuration:0.3 animations:^{
        containerView.frame = CGRectMake(0.0f, -kKeyboardOffsetY, containerView.frame.size.width, containerView.frame.size.height);
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
    [UIView animateWithDuration:0.3 animations:^{
        containerView.frame = CGRectMake(0.0f, self.view.frame.origin.y, containerView.frame.size.width, containerView.frame.size.height);
    }];
}

您不必使用'viewWithTag'方法,只需要获取对容器视图的引用,也可以通过迭代子视图来完成。

答案 1 :(得分:0)

键盘出现时调整桌面视图的高度。

以下是我的一个项目的例子。

首先:听一下键盘事件:

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //Be informed of keyboard
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];    
}



#pragma mark keyboard

- (void)keyboardDidShow:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGRect frame = CGRectMake(self.tableView.frame.origin.x,
                              self.tableView.frame.origin.y,
                              self.tableView.frame.size.width,
                              self.tableView.frame.size.height - size.height);
    self.tableView.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
                                      self.tableView.frame.origin.y,
                                      self.tableView.frame.size.width,
                                      self.tableView.frame.size.height + size.height);
}

答案 2 :(得分:0)

我是这样做的,它是动画的所以看起来不错。

键盘高度为216px,停止时需要0.25秒。

以下是我的代码中的部分:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == 2) {
    void (^a)(void) = ^(void){
        [UIView animateWithDuration:.25 animations:^{
            RegNrView.frame = CGRectMake(0, -216, 320, 460);
        }
        completion:nil];
    };

    [[NSOperationQueue mainQueue] addOperationWithBlock:a];
}

}

这使得UIView和键盘在同一时间移动。