使用键盘ios向上/向下移动视图

时间:2015-02-02 04:43:11

标签: ios objective-c iphone

我正在开发类似于whatsapp等的聊天应用程序。它在视图控制器中有一个tableview,在底部工具栏中有一个文本字段和按钮。我遇到了向上滑动视图的各种问题,并使用此link我设法将视图向上滑动。但是我想要关闭键盘并且视图下降并适合屏幕。我尝试使用轻击手势并单击返回按钮但似乎没有任何效果。如何使视图向下滑动,键盘消失?

此外,如何更改文本字段的宽度,以便在用户编写邮件时可以显示多行?

3 个答案:

答案 0 :(得分:1)

您可以将tap手势事件添加到tableview单元格,也可以在用户点击tableview时使用触摸事件方法,然后根据键盘之前的状态显示或隐藏键盘。希望这对你有所帮助。

答案 1 :(得分:0)

使用textFieldShouldReturn重新设置第一响应者状态(关闭键盘)并向上滑动视图。

我个人这样做:

  1. 我注册了通知,知道什么时候会显示键盘,以及什么时候会被隐藏。

  2. 当键盘出现时,我设置了视图插图以包括键盘的大小。

  3. 向上滑动视图

  4. 当键盘消失时,我将插图设置为零。

  5. TextField Delegate点按“返回”按钮时隐藏键盘的方法

    -(BOOL)textFieldShouldReturn:(UITextField*)textField;
    {
        [textField resignFirstResponder];
        return NO; // We do not want the UITextField to insert line-breaks.
    }
    

    注册键盘显示/消失通知

    - (void)viewDidLoad
    {
        ...
        // Register for notifications for when the keyboard will appear and disappear 
        [self registerForKeyboardNotifications];
    }
    
    // Call this method somewhere in your view controller setup code.
    - (void)registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                  selector:@selector(keyboardWillBeHidden:)
                                                      name:UIKeyboardWillHideNotification object:nil];
    
    }
    
    // Called when the UIKeyboardDidShowNotification is sent.
    // Original code for this part here: http://stackoverflow.com/a/16044603/4518324
    - (void)keyboardWasShown:(NSNotification *)note 
    {
        NSDictionary *userInfo = note.userInfo;
        NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
        CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
    
        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
            self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
        } completion:nil];
    }
    
    - (void)keyboardWillBeHidden:(NSNotification *)note 
    {
        NSDictionary *userInfo = note.userInfo;
        NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
        CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
    
        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
            self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
        } completion:nil];
    }
    

    我创建的示例代码涉及在显示或关闭键盘时调整视图大小。

    https://github.com/gingofthesouth/KeyboardHideShow

答案 2 :(得分:0)

我做对了。当键盘被解除时,我根据需要调整视图框架时调用另一个方法,即View.frame-keyboard.frame.height。不管怎样,谢谢!:)