移动键盘的视图然后按home,返回查看非移位视图的结果?

时间:2012-06-22 10:11:07

标签: ios xcode uikeyboard

我使用下面的代码atm。只要您在键盘启动时不退出应用程序(按主页),它就可以正常工作。所以我想,简单,只是在viewWillDissappear中的resignFirstResponder :( BOOL)动画,但是这显然不会被称为按住家...

如此快速回顾问题场景: 点按textView - >键盘出现,视图移动 按回家 再次打开应用程序 - >键盘仍然向上,视图返回原位,因此内容不可见

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }

- (void) keyboardWillShow: (NSNotification*) aNotification;
    {       
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        CGRect rect = [[self view] frame];
        rect.origin.y -= 60; 
        [[self view] setFrame: rect];
        [UIView commitAnimations];
    }

    - (void) keyboardWillHide: (NSNotification*) aNotification;
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        CGRect rect = [[self view] frame];
        rect.origin.y += 60;
        [[self view] setFrame: rect];
        [UIView commitAnimations];
    }

1 个答案:

答案 0 :(得分:0)

您可以为输入背景事件添加通知,如下面的代码

 - (void)viewDidLoad
{
    //Add this 
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(handleEnteredBackground:) 
                                                 name: UIApplicationDidEnterBackgroundNotification
                                               object: nil];
}

在函数中

- (void) handleEnteredBackground:(NSObject*)obj
{
    //Adjust your views   
}