UITextField键盘激活时移动视图



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

我可以删除这个问题吗。Jeez

正如我所理解的问题,当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"方法,只需获得对容器视图的引用,这也可以通过迭代子视图来完成。

当键盘出现时调整表视图的高度。

这是我的一个项目的一个例子。

第一:监听键盘事件:

-(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);
}

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

键盘的高度是216像素,需要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和键盘可以同时移动。

最新更新