IOS:键盘出现时向上移动TextField

时间:2016-10-17 02:42:00

标签: ios keyboard textfield

当键盘出现时,我使用以下方法向上移动视图,因此键盘不会阻止文本字段。基本上,我将文本字段嵌入到滚动视图中,并在键盘出现时向上滚动。

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
        [_scrollView scrollRectToVisible:_activeField.frame animated:YES];
    }
}

但是,当文本字段已经位于视图的顶部并且不需要它跳起时,上面的代码会使它跳出视图。

在某个地方,我记得读过你可以添加一行来防止这种情况,但我记不住了。基本上,它会测试文本字段是否已经足够高,键盘不会覆盖它,因此,视图不需要移动。

上述代码中的if语句似乎没有筛选出未隐藏文本字段的情况。

有人可以建议这样做吗?

谢谢。

3 个答案:

答案 0 :(得分:0)

如果使用自动布局,则应使用约束来操纵视图而不是帧。您可以尝试根据需要操作textField或视图的底部间距,并将layoutIfNeeded调用放在动画块中。您还可以检查底部约束的值是否已经是特定值,或者在UIKeyboardWillHideNotificationUIKeyboardWillShowNotification上传递bool,如果是,则约束的值不应更改。类似的东西:

-(void)showViewAnimatedly:(BOOL)show{

    if(show){
        [bottomConstraint setConstant:0];
    }else{
        [bottomConstraint setConstant:-160];
    }

    [UIView animateWithDuration:0.3f animations:^{

        [self.view layoutIfNeeded];

    }];
}

我还建议使用UIKeyboardWillShowNotification代替UIKeyboardDidShowNotification,因为转换似乎与键盘出现更加一致。 UIKeyboardDidShowNotification将在键盘出现后开始转换。

答案 1 :(得分:0)

查看' IQKeyboardManager'。它是您出色的控制要求。它只需要一行代码。它适用于UITextFields和UITextViews。 它还可以选择上一个和下一个按钮。您可以将其作为cocoapod

安装在项目中

https://www.cocoacontrols.com/controls/iqkeyboardmanager

答案 2 :(得分:0)

当键盘出现时,如果需要,可以使用动画更改textview或textfield的y轴坐标。您可以参考的代码位于---->

之下
-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}

//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

    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.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

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

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}