移动键盘的视图,然后按 home、backs 查看结果为非移动视图



我使用下面的代码atm。只要您在键盘打开时不退出应用程序(按主页),它就可以正常工作。所以我想,很简单,只是辞职第一响应者在视野中会消失:(布尔)动画,但这显然不会被称为压回家......

因此,快速回顾问题场景:点击文本视图 ->键盘出现,视图移动按主页再次打开应用程序 ->键盘仍然打开,视图回到原来的位置,因此内容不可见

 - (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];
    }

您可以为输入后台事件添加通知,如以下代码

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

而在函数中

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

最新更新