同时关闭带有视图的键盘

Posted

技术标签:

【中文标题】同时关闭带有视图的键盘【英文标题】:Dismiss keyboard with view simultaneously 【发布时间】:2014-09-24 02:14:19 【问题描述】:

我正在构建的应用程序有一个消息/聊天视图控制器,它有一个带有 textView 的视图和一个位于视图底部的按钮(标签栏设置为隐藏)。当视图加载时,其中包含这些对象(textView 和按钮)的视图位于屏幕的底部(就像您进入对话时它在 iMessage 应用程序中的外观一样)

现在,当点击 textview 时,显然键盘会向上移动,与此同时,我设法让视图向上移动并直接坐在键盘顶部(就像你输入一个新的短信)。

现在我要做的是让视图通过键盘上下移动。截至目前,它从底部开始。当键盘升起时,该视图会在底部停留一两秒钟,然后仅出现在键盘上方。键盘关闭时也是如此。

我希望我能提供图片,但我想我的名声不太好

有没有一种方法可以匹配两者的过渡或动画,使它们像连接一样一起移动?

视图已初始化

//Create the keyboard UIView
    self.keyboardView =  [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y + self.view.frame.size.height - (self.navigationController.tabBarController.tabBar.frame.size.height), self.view.frame.size.width, 50)];

用于移动视图的选择器

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

选择器实现

- (void)keyboardFrameDidChange:(NSNotification*)notification
    NSDictionary* info = [notification userInfo];

    CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        [self.keyboardView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y - self.keyboardView.frame.size.height, 320, self.keyboardView.frame.size.height)];
        self.myScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.myTableView.frame.size.height);



【问题讨论】:

【参考方案1】:

你可以在这里做的是键盘通知

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWasShown:)

                                             name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWillBeHidden:)

                                             name:UIKeyboardWillHideNotification object:nil];

keyboardWasShown

- (void)keyboardWasShown:(NSNotification*)aNotification


NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
double duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
CGRect rect=self.viewChatWindow.frame;
rect.size.height=self.viewChatWindow.frame.size.height-kbSize.height;
self.viewChatWindow.frame=rect;

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(scrollToLast)];
[UIView commitAnimations];


keyboardWillBeHidden

- (void)keyboardWillBeHidden:(NSNotification*)aNotification


double duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
CGRect rect=self.viewChatWindow.frame;
rect.size.height=self.view.frame.size.height-rect.origin.y;
self.viewChatWindow.frame=rect;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(scrollToLast)];
[UIView commitAnimations];

【讨论】:

【参考方案2】:

只需将您的 [keyboardView setFrame:...] 包装在动画块中即可。您可以从通知对象中获取键盘的动画时长和曲线:

NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:curve animations:^
    self.keyboardView.frame = ...;
 completion:nil];

【讨论】:

如果我用这个方法来关闭键盘怎么办self.myScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; 嗯,好问题。我对 UIScrollViewKeyboardDismissModeInteractive 不是很熟悉。看起来这可能是 ***.com/questions/19118921/… 的副本。您是否尝试过那里描述的解决方案? 我已经尝试过了,如果我用它的自然动画来关闭键盘,它就可以完美地工作。所以谢谢你!【参考方案3】:

使用 UITextFieldDelegate

这将是你的键盘将出现的标志

-(void)textFieldDidBeginEditing:(UITextField *)textField
//Animate like slide up.
        [UIView animateWithDuration:0.3
                         animations:^
                             //Use offset rather than contentSize
                             scrollView.contentOffset = CGPointMake(x,y);
                         ];

当您的键盘被关闭时 (resignFirstResponder)

-(void)dismissKeyboard
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.3
                     animations:^
                         scrollView.contentOffset = CGPointMake(0,0);

                     ];
 

【讨论】:

但是我正在使用 textView...如果我使用的是文本字段,这不是很理想吗? 我认为这与 textview 相同,但 UITextViewDelegate 代替。

以上是关于同时关闭带有视图的键盘的主要内容,如果未能解决你的问题,请参考以下文章

关闭时,横向iPad上带有键盘的模态视图控制器会更改位置[重复]

结合点击关闭键盘、键盘避免视图和提交按钮

解除键盘后,带有所选文本的UITextView不响应触摸

如何同时关闭 UIAlertController 和键盘?

推动视图控制器时防止键盘关闭

如果同时使用自动键盘隐藏,则文本字段的清除按钮 (x) 不起作用