【问题标题】:Move View up/down with Keyboard ios使用键盘 ios 向上/向下移动视图
【发布时间】:2015-02-02 22:17:02
【问题描述】:

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

此外,如何更改文本字段的宽度,以便在用户编写消息时出现多行?

【问题讨论】:

  • 尝试使用 textfielddidbeginediting 和 textfielddidendediting 委托方法

标签: ios objective-c iphone


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    我个人是这样做的:

    1. 我注册通知以了解键盘何时显示以及何时隐藏。

    2. 当键盘出现时,我将视图插图设置为包含键盘的大小。

    3. 向上滑动视图

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

    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

    【讨论】:

    • 我没有在任何地方使用滚动视图。 :\
    • 我已经更新了标准 UIView 的示例代码并创建了一个示例项目来演示它(包括 github 链接)。
    • 至于你的第二个问题,如果你想要多行,你需要使用 UITextView。
    【解决方案3】:

    我猜对了。当键盘被关闭时,我调用了另一种方法,该方法根据要求适合视图框架,即 View.frame-keyboard.frame.height。无论如何,谢谢!:)

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2015-11-29
      • 1970-01-01
      • 2014-01-27
      • 2018-05-21
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      相关资源
      最近更新 更多