我正在开发 iPad 应用程序。在其中一个 View 中,我有一个 subview ,它在按钮点击事件中出现和消失。 subview 包含一个 UITextView
。默认情况下,我将其设置为第一响应者,以便键盘在 View 出现时立即出现。当 UIKeyboardWillHideNotification
被触发时, subview 也会消失,即键盘被隐藏。
现在,问题是一旦应用程序旋转,系统就会触发 UIKeyboardWillHideNotification
,这又会使 subview 消失。我希望键盘保留在屏幕上。
这是怎么回事,我该如何解决!?
注意: View 和 subview 都有独立的 View Controller 。在 subview 的 View Controller 类中接收到 UIKeyboardWillHideNotification。
最佳答案
您可以在 shouldAutoRotate 方法中声明一个 BOOL 变量并在它被调用时设置,然后在用于显示和隐藏 subview 的选择器方法中您可以使用一个简单的 if 条件来判断天气 View 是否旋转。
像这样:
if(viewRotated)
{
subView.hidden = YES;
}
viewRotated = NO;
编辑部分:
我不确定这段代码发生了什么,但它在我的一个应用程序中运行得很好,它的 ipad 编码是由我的 friend 完成的。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillShowNotification
object:nil];
}
else
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
}
return YES;
}
如果您的 UIKeyboardWillHideNotification 没有触发,您可以再次添加通知,方法是在此方法中再次添加这些通知。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
关于ios - 键盘在旋转时隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6435050/