Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在导航项';s的搜索栏有焦点_Ios_Uiviewcontroller_Uisearchcontroller - Fatal编程技术网

Ios 如何在导航项';s的搜索栏有焦点

Ios 如何在导航项';s的搜索栏有焦点,ios,uiviewcontroller,uisearchcontroller,Ios,Uiviewcontroller,Uisearchcontroller,我更新了我的应用程序,以处理导航栏下搜索栏的新iOS 11搜索行为: - (void)viewDidLoad { [super viewDidLoad]; UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.navigationItem.searchController = searchControlle

我更新了我的应用程序,以处理导航栏下搜索栏的新iOS 11搜索行为:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.navigationItem.searchController = searchController;

    // Those are optional, but that's what I want my UI to look like
    searchController.hidesNavigationBarDuringPresentation = NO;
    searchController.obscuresBackgroundDuringPresentation = NO;
    self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
我的视图上还有一个按钮,点击该按钮将关闭当前视图控制器:

- (IBAction)dismiss:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
问题是:当用户点击按钮时,如果searchController的搜索栏具有焦点(即是第一响应者),则视图控制器不会被解除

  • 第一次点击按钮时,搜索栏失去焦点,键盘隐藏(好像调用了
    [searchBar resignFirstResponder]
  • 在第二次点击时,视图控制器最终被解除


如何使第一次点击按钮立即关闭视图控制器(作为副作用移除搜索栏的焦点)?

立即关闭视图控制器的解决方案是使用
UISearchController


立即关闭视图控制器的解决方案是使用
UISearchController


就我而言,解决办法是:

UIView.setAnimationsEnabled(false)
searchController.isActive = false
UIView.setAnimationsEnabled(true)

然后,您可以通过委派或直接解除viewController。

在我的情况下,解决方法是:

UIView.setAnimationsEnabled(false)
searchController.isActive = false
UIView.setAnimationsEnabled(true)
然后,您可以通过代理或直接关闭viewController。

在我的场景中,我有一个UITableViewController,带有一个搜索栏作为覆盖。搜索结果位于单独的UITableViewController中。现在我想在显示搜索结果时取消覆盖@纪尧姆·阿尔吉斯你的解决方案对我也很有效

Swift 4中的语法:

    self.navigationItem.searchController?.isActive = false
    self.dismiss(animated: true) {}
在我的场景中,我有一个UITableViewController,其中有一个搜索栏作为覆盖。搜索结果位于单独的UITableViewController中。现在我想在显示搜索结果时取消覆盖@纪尧姆·阿尔吉斯你的解决方案对我也很有效

Swift 4中的语法:

    self.navigationItem.searchController?.isActive = false
    self.dismiss(animated: true) {}
Swift 4.2

self.searchController.dismiss(animated: false, completion: nil)
self.dismiss(animated: true, completion: {
   ///
})
Swift 4.2

self.searchController.dismiss(animated: false, completion: nil)
self.dismiss(animated: true, completion: {
   ///
})

所有上述方法都会导致使用空字符串调用
updateSearchResults

根据您的实现,这可能会导致重新加载tableview,这可能会损害用户体验

以下是我围绕这个问题所做的工作:

self.presentingViewController?.dismiss(animated: true, completion: nil)


所有上述方法都会导致使用空字符串调用
updateSearchResults

根据您的实现,这可能会导致重新加载tableview,这可能会损害用户体验

以下是我围绕这个问题所做的工作:

self.presentingViewController?.dismiss(animated: true, completion: nil)


很好,谢谢!Swift 5,iOS 13工作正常,谢谢!Swift 5,iOS 13