旋转隐藏键盘 [英] Keyboard hides on rotation

查看:82
本文介绍了旋转隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iPad应用程序.在其中一个视图中,我有一个子视图,该子视图在按钮轻击事件中出现和消失.子视图包含UITextView.默认情况下,我将其设置为第一响应者,以便在视图显示后立即显示键盘.触发UIKeyboardWillHideNotification时,即隐藏键盘时,子视图也会消失.

I'm working on an iPad application. In one of it's views, i have a subview which appears and disappears on button tap event. The subview contains a UITextView. By default i make it the first responder so that the keyboard appears as soon as the view appears. The subview also disappears when UIKeyboardWillHideNotification is fired i.e. keyboard is hidden.

现在,问题在于,一旦旋转应用程序,系统就会触发UIKeyboardWillHideNotification,这反过来又使子视图消失.我希望键盘保留在屏幕上.

Now, the problem is that as soon as the application is rotated, UIKeyboardWillHideNotification is fired by the system, which in turn make the subview disappear. I want the keyboard to remain on screen.

这是怎么回事,我该如何解决!!

What's going on, and how can i fix it!?

注意:视图和子视图都有单独的视图控制器.在子视图的视图控制器类中收到UIKeyboardWillHideNotification.

Note: Both the view and subview have separate view controllers. UIKeyboardWillHideNotification is received in subview's view controller class.

推荐答案

您可以在shouldAutoRotate方法中声明一个BOOL变量,该变量在调用时设置,然后在选择器方法中显示和隐藏子视图,您可以使用如果天气视图是否旋转,则很简单.

You can declare a BOOL variable in shouldAutoRotate method and that is set when it is called and then in the selector method for showing and hiding the subview you can use a simple if condition that weather view is rotated or not.

像这样:

if(viewRotated)
{
    subView.hidden = YES;
}
viewRotated = NO;

编辑部分:
我不确定这段代码中发生了什么,但是它在我的一个由我的朋友完成ipad编码的应用程序中工作得很好.

Edit Part:
I am not sure whats going on in this code but its working perfectly in one of my apps who's ipad coding was done by my 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没有被触发,您可以通过在此方法中再次添加这些通知来再次添加通知.

And you can add the notification again if your UIKeyboardWillHideNotification is not fired by add those notification again in this method.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

这篇关于旋转隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆