【问题标题】:Move view when so that keyboard does not hide text field当键盘不隐藏文本字段时移动视图
【发布时间】:2011-08-23 08:41:06
【问题描述】:

在我的应用程序中,当我单击文本字段时,键盘会将其隐藏。请帮帮我——当我点击文本字段时,如何向上移动我的视图。我在textFieldDidBeginEditing:使用这个代码

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

但它不起作用。

【问题讨论】:

标签: cocoa-touch ios keyboard uitextfielddelegate


【解决方案1】:

您不应该相信textFieldDidBeginEditing: 会针对键盘进行调整,因为即使用户使用不会显示屏幕键盘的物理键盘键入,也会调用此方法。

改为收听UIKeyboardWillShowNotification,它仅在实际显示键盘时触发。您需要执行三个步骤:

  1. 从通知userInfo 字典中确定键盘的实际大小。大小会因横向/纵向以及不同的设备而有所不同。
  2. 使用确定的大小更新contentInset。您可以制作动画,通知甚至会告诉您键盘动画的持续时间。
  3. 将文本字段滚动到视图中,很容易忘记这一点!

您可以从here找到更多信息和示例代码

【讨论】:

  • 请给我看代码以了解横向和波特兰键盘的大小
  • @Sandy:完整的代码和工作示例可在我在答案中链接到的 developer.apple.com 上的链接中找到。
  • 请记住,Apple 示例忘记使用文本字段的高度。导致与键盘相邻的字段未显示或未完全显示。
【解决方案2】:

您可以执行以下操作,但首先确保您已将 UITextField 委托设置为您自己并且

#define kOFFSET_FOR_KEYBOARD 350;

在顶部。这是您希望视图移动的距离

//method to move the view up/down whenever the keyboard is shown/dismissed

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.

        if (rect.origin.y == 0 ) {
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            //rect.size.height += kOFFSET_FOR_KEYBOARD;
        }

    }
    else
    {
        if (stayup == NO) {
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
    }
    self.view.frame = rect; 
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMovedUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMovedUp:YES];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    stayup = YES;
    [self setViewMovedUp:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    stayup = NO;
    [self setViewMovedUp:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

【讨论】:

  • 这个例子有几个缺点,基于一些错误的假设;永远不会有物理键盘,只有纵向模式,Apple 永远不会改变键盘的大小或动画的长度。如果用户想在不先关闭键盘的情况下在文本字段之间进行更改,也可能无法滚动到偏移的内容。
  • thanx 但我不想移动所有文本字段的视图,所以请告诉我如何选择特定的文本字段
  • 你好,我不知道如何阻止我的视图向上移动键盘不隐藏我的文本字段。请帮助我
  • #define 后面不应有分号
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多