Hide keyboard in applicationDidEnterBackground: -

2019-05-10 03:38发布

When the home button gets pressed I want to hide the keyboard and restore my view to a sane state, so that when the app is started/foregrounded again, there is no textfield selected and the keyboard is hidden.

My app delegate has this implementation of the method:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [tabBarController.view endEditing:YES];
}

The keyboard does indeed get removed, but what I don't get is this: Apple's docs say that a snapshot of the app is taken after the method returns. However this poses a problem with the keyboard slide-down animation. Sometimes when the app is started again for a short moment it shows the keyboard half-way down. I assume the screenshot is taken before the animation was finished.

What would I do to solve this, introduce a short timer in the applicationDidEnterBackground: method? I wonder if there is a "cleaner" solution.

1条回答
The star\"
2楼-- · 2019-05-10 04:19

I've not tried this but what about turning animations off just before you resign the keyboard:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [UIView setAnimationsEnabled:NO];
     [tabBarController.view endEditing:YES];
}

If this works you need to turn them back on in applicationWillEnterForeground

查看更多
登录 后发表回答