【问题标题】:Moving view when UITextField Keyboard gets activated [duplicate]UITextField键盘被激活时移动视图[重复]
【发布时间】:2013-08-01 17:36:30
【问题描述】:

当我激活我的UITextField 时会出现以下问题(如果我只显示几个图像而不是尝试将 UIViewwhich is nested within myUITableViewController` 作为页脚。

哎呀,我可以删除这个问题吗?天哪

【问题讨论】:

  • 请在发布另一个重复问题之前查看相关问题。
  • TPKeyboardAvoiding 课程将为您服务
  • @rmaddy ok ok jeez 我在第三次投票后得到它。
  • 发生了什么问题?

标签: ios objective-c uitextfield


【解决方案1】:

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

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

首先:监听键盘事件:

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

【讨论】:

  • 您仍然需要将您的班级添加为键盘事件的观察者
  • 感谢@Eugene。我更新了。
【解决方案2】:

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

键盘高度为 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 和键盘同时移动。

【讨论】:

  • 您不应该对持续时间或帧进行硬编码。两者都可能改变。如果文本字段具有输入附件视图,则框架将有所不同。 Apple 提供的示例代码显示了使用正确的键盘事件执行此操作的正确方法。
【解决方案3】:

据我了解,当 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”方法,只需要获取对容器视图的引用,这也可以通过遍历子视图来完成。

【讨论】:

    猜你喜欢
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2014-02-07
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多