我们打开了一个显示键盘的视图,但是当单击后退按钮时,视图从右侧滑出,而键盘只有在视图消失时才会滑动。如果我们在 viewwilldisappear 时调用 resignFirstResponder,view 会向右滑动,同时键盘向下滑动。是否可以让键盘随视图滑出?
问问题
1173 次
4 回答
1
没有标准的方法来做你想做的事,但是......
基本上,键盘只是一个视图,呈现在它自己的 UIWindow 中,位于所有其他窗口之上。
因此,理论上,您需要做的是找到键盘视图并将其移动到所需的方向。我认为你应该使用transform
属性,不要搞砸frame
.
Class keyboardClass = NSClassFromString(@"UIPeripheralHostView");
for ( UIWindow *window in [[UIApplication sharedApplication] windows] ) {
for ( UIView *subview in window.subviews ) {
if ( [subview isKindOfClass:keyboardClass] ) {
// that's keyboard
}
}
}
已编辑:
如果您在谈论 UINavigationController 并且它是推送/弹出期间的默认幻灯片动画,那么您只需要在文本视图中调用resignFirstResponder
inviewDidDisappear
和becomeFirstResponder
in即可。viewWillAppear
这样,您的键盘将随着您的视图滑动。
于 2012-04-27T05:45:53.893 回答
1
我已经对此进行了测试,它适用于 iOS 5.1,但是,我认为这不是推荐的行为。
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
if ([[keyboardWindow description] hasPrefix:@"<UITextEffectsWindow"]) {
NSLog(@"%@", [keyboardWindow description]);
[UIWindow beginAnimations:@"fadeKeyboard" context:nil];
keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height);
[UIWindow commitAnimations];
}
您还可以使用通知 UIKeyboardWillHideNotification 来检测键盘何时要隐藏,或者手动使用上面的代码。
于 2012-04-27T05:59:38.017 回答
0
尝试坚持resignFirstresponder
使用该viewDidDisappear
方法。
于 2012-04-27T04:19:17.583 回答
0
**Set notificatins and use these methods.....Hope it solve problem:
First of all set your whole view in scrollView**
-(void)keyboardDidHide:(NSNotification *)notif
{
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height);
}];
keyboardVisible=NO;
}
-(void)keyboardDidShow:(NSNotification *)notif
{
scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235);
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
[scrollView setContentOffset:CGPointMake(0,162) animated:YES];
}];
keyboardVisible=YES;
}
**In viewDidLoad() add this**
//keyboard
scrollOriginalFrame=self.view.frame;
scrollOriginalFrame.size.height-=103;
scrollView.contentSize=scrollOriginalFrame.size;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
keyboardVisible=NO;
于 2012-04-27T04:35:20.383 回答