iPhone:应用程序上的隐藏键盘是否进入背景或视图是否消失

iPhone:应用程序上的隐藏键盘是否进入背景或视图是否消失,iphone,ios,uisearchbar,resignfirstresponder,Iphone,Ios,Uisearchbar,Resignfirstresponder,我有一个UISearchBar,单击它时会显示键盘。 但是,如果用户在显示键盘的同时按下home(主页)按钮,然后返回应用程序,键盘仍然可见。 当应用程序关闭/进入后台时,如何隐藏键盘 我在ViewDidEnglish中尝试了以下方法: [eventSearchBar resignFirstResponder]; [eventSearchBar endEditing:YES]; 我还在appDidEnterBackground中的委托中尝试了这一点: [self.rootController

我有一个UISearchBar,单击它时会显示键盘。 但是,如果用户在显示键盘的同时按下home(主页)按钮,然后返回应用程序,键盘仍然可见。 当应用程序关闭/进入后台时,如何隐藏键盘

我在ViewDidEnglish中尝试了以下方法:

[eventSearchBar resignFirstResponder];

[eventSearchBar endEditing:YES];
我还在appDidEnterBackground中的委托中尝试了这一点:

[self.rootController.navigationController.view endEditing:YES];

这些都不起作用。

您可以在appDelegate中执行此操作

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

在视图控制器中,例如在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];

我的所有标准方法都会偶尔失败。到目前为止,这是我获得可靠结果的唯一途径

在最上面的控制器中

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
}

-(void) willResignActiveNotification:(NSNotification*) vNotification {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [self setEditing:NO];
}
有一种奇怪的情况,文本字段将不再响应
辞职FirstResponder
endEditing
,但仍具有键盘

Swift版本:

func applicationDidEnterBackground(application: UIApplication) {
    window?.endEditing(true)
}

swift 3.2版本中的解决方案

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(hideTextField), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    func hideTextField(){
        eventSearchBar.endEditing(true)
    }

最好的方法是将window?置于AppDelegate上的applicationWillResignActive中的endEditing(true):

func applicationWillResignActive(_ application: UIApplication) {
    window?.endEditing(true)
}

你在willEnterBackground中试过吗?我认为没有willEnterBackground方法有一个
applicationWillResignActive:
方法。阅读以下内容:谢谢,我尝试过用这种方法辞职FirstResponder和Endedit,但仍然没有成功,rootController.navigationController.view调用它的正确方式是吗?顺便说一句,rootController的类型是TabBarController
ViewWillEnglish
在应用程序进入后台时不会被调用。这可能是一个品味问题。我更喜欢在其应用的视图控制器中本地执行此操作。这是您问题的唯一解决方案…因为当应用程序最小化时,不会调用viewwillapper或viewwillapper…如果要执行此操作,则必须在ApplicationIdentinterBackground方法中执行此操作..或者必须在中访问您的视图ApplicationIdentinterBackground…然后您可以执行[self.view endEditing:YES];。。。将此方法放在APPDELEGATE中。我更喜欢此解决方案,因为它仅与所讨论的视图隔离。@Dangytagart:是的,我就是这么认为的。谢谢你的反馈!