iOS 在视图更改时关闭键盘
我有一个基于视图的应用程序,在其中一个子视图中有一个 UIScrollView。我编写了处理程序来在键盘出现和消失时调整滚动视图的大小。我希望在用户离开视图时关闭键盘,因此我在 viewWillDisappear
中调用 [currentField resignFirstResponder]
。这会关闭键盘,但不会调用处理程序来调整滚动视图的大小(当我在其他地方调用相同的代码时,它会这样做)。有什么建议吗?
编辑:这些是我使用的处理程序:
-(void) keyboardWasShown:(NSNotification*) notification
{
if(keyboardShown)
return;
NSDictionary* info=[notification userInfo];
NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize=[value CGRectValue].size;
CGRect viewFrame=[scrollView frame];
viewFrame.size.height-=keyboardSize.height;
scrollView.frame=viewFrame;
keyboardShown=YES;
}
-(void) keyboardWasHidden:(NSNotification*) notification
{
NSDictionary* info=[notification userInfo];
NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize=[value CGRectValue].size;
CGRect viewFrame=[scrollView frame];
viewFrame.size.height+=keyboardSize.height;
scrollView.frame=viewFrame;
keyboardShown=NO;
}
当我在其他地方调用 [currentField resignFirstResponder]
时,它会毫无问题地调用处理程序。
I have a view-based application, and in one of the subviews there is a UIScrollView. I have written handlers to adjust the size of the scroll view when the keyboard appears and disappears. I would like the keyboard to be dismissed when the user leaves the view, so I call [currentField resignFirstResponder]
in viewWillDisappear
. This dismisses the keyboard, but does not call the handler to resize the scroll view (when I call the same code in other places, it does). Any suggestions?
EDIT: These are the handlers that I use:
-(void) keyboardWasShown:(NSNotification*) notification
{
if(keyboardShown)
return;
NSDictionary* info=[notification userInfo];
NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize=[value CGRectValue].size;
CGRect viewFrame=[scrollView frame];
viewFrame.size.height-=keyboardSize.height;
scrollView.frame=viewFrame;
keyboardShown=YES;
}
-(void) keyboardWasHidden:(NSNotification*) notification
{
NSDictionary* info=[notification userInfo];
NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize=[value CGRectValue].size;
CGRect viewFrame=[scrollView frame];
viewFrame.size.height+=keyboardSize.height;
scrollView.frame=viewFrame;
keyboardShown=NO;
}
When I call [currentField resignFirstResponder]
anywhere else, it calls the handler without problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,在发布
UIKeyboardDidHideNotification
之前,您已被删除为观察者,很高兴我能提供帮助。但是观察UIKeyboardWillHideNotification
和UIKeyboardWillShowNotification
可能足以了解您对键盘的反应。键盘通知有一个用户信息键UIKeyboardAnimationDurationUserInfoKey
,您可以使用它通过键盘动画对帧调整进行动画处理。如果您不将视图设置为新位置,这可以避免您的视图产生“沉闷”的感觉。以下是您可以执行的操作的简单示例:现在您只需添加此方法作为两个通知的目标:
So you were being removed as observer before
UIKeyboardDidHideNotification
was posted, glad I could help. But observing theUIKeyboardWillHideNotification
andUIKeyboardWillShowNotification
is probably enough for your reaction to the keyboard. The keyboard notifications have a user info keyUIKeyboardAnimationDurationUserInfoKey
which you can use to animate your frame adjustments with the keyboard animations. This avoids the 'clunk' feeling your views will have if you don't animate them to new positions. Here is a quick example of what you can do:Now you simply add this method as the target for both notifications: