移动视图,以便键盘不遮挡文本框。

4
在我的应用中,当我点击文本框时,键盘会将其隐藏。请帮助我,当我点击文本框时,如何将我的视图向上移动。我在textFieldDidBeginEditing:中使用了以下代码。
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

但是它不起作用。

可能是重复的问题:如何在键盘出现时使UITextField上移 - jscs
2个回答

13

不要信任textFieldDidBeginEditing:来调整键盘,因为即使用户使用物理键盘输入,在屏幕上不会显示虚拟键盘,该方法也会被调用。

相反,监听UIKeyboardWillShowNotification,该通知只有在实际显示键盘时才会触发。您需要执行三个步骤:

  1. 从通知的userInfo字典中确定键盘的实际大小。尺寸将根据横向/纵向和不同设备而异。
  2. 使用确定的大小更新contentInset。您可以进行动画处理,通知甚至会告诉您键盘动画的持续时间。
  3. 滚动文本字段以查看其内容,这很容易被忽视!

您可以从此处找到更多信息和示例代码。


请向我展示代码以了解横向和纵向键盘的大小。 - sandy
@Sandy:完整的代码和演示可在我在回答中提供的 developer.apple.com 上的链接中获取。 - PeyloW
请注意,苹果的示例忘记了处理文本字段的高度。这会导致边界与键盘接触的字段未显示或未完全显示。 - ophychius

1
你可以做以下的事情,但首先确保你已将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];
}

4
这个例子有几个缺点,基于一些错误的假设:没有物理键盘,只有竖屏模式,苹果永远不会改变键盘的大小或动画长度。如果用户想要在不先取消键盘的情况下在文本字段之间进行切换,也可能无法滚动到偏移的内容。 - PeyloW
谢谢,但我不想为所有文本字段移动我的视图,所以请告诉我如何选择特定的文本字段。 - sandy
你好,我不知道如何防止我的视图向上移动,以便键盘不会遮挡我的文本框。请帮帮我。 - sandy
1
一个 #define 后面不应该有分号。 - Toad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接