在您的视图控制器中,例如在 init 方法中,注册UIApplicationWillResignActiveNotification
:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
当应用程序进入后台时,使搜索显示控制器处于非活动状态。这会从搜索字段中移除焦点并隐藏键盘:
- (void)willResignActive:(NSNotification *)note
{
self.searchDisplayController.active = NO;
// Alternatively, if you only want to hide the keyboard:
// [self.searchDisplayController.searchBar resignFirstResponder];
}
并且不要忘记在 dealloc 方法中删除观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillResignActiveNotification
object:nil];