1

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

快速回顾一下问题场景:点击 textView -> 键盘出现,视图被转移 按 home 再次打开应用程序 -> 键盘仍然打开,视图回到原来的位置,所以内容不可见

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

1 回答 1

0

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

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

并且在函数中

- (void) handleEnteredBackground:(NSObject*)obj
{
    //Adjust your views   
}
于 2012-06-22T10:18:54.080 回答