UISearchController searchBar显示取消button不被尊重

我已经添加了一个UISearchController到我的应用程序,并将其设置为searchBar到我的navigationItemtitleView

这工作,但我看到取消button,尽pipe已设置showsCancelButton取消button为false

  searchController = UISearchController(searchResultsController: searchResultsController) searchController.searchResultsUpdater = searchResultsUpdater // Configure the searchBar searchController.searchBar.placeholder = "Find Friends..." searchController.searchBar.sizeToFit() searchController.searchBar.showsCancelButton = false self.definesPresentationContext = true navigationItem.titleView = searchController.searchBar 

在这里输入图像说明

这似乎是iOS中的一个错误。 我所描述的同样的行为可以在Apple提供的示例项目中看到

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

该文件指出,这是默认值为NO但似乎并非如此。 设置showsCancelButtonNO似乎没有效果。

我已经为此提出了一个雷达,我正在等待回听。

我同意,这似乎是一个错误。 问题是searchController不断重新设置showsCancelButton属性。 我find了一个解决scheme,涉及:

  1. UISearchBar以忽略setShowsCancelButton
  2. 要使searchController使用该子类,您必须inheritanceUISearchController
  3. 然后你发现searchBar没有触发search控制器的委托方法,所以你必须分别触发它们…

令人费解的,但它似乎是伎俩。 你可以在这里find完整的答案 。

Swift3中简单的解决scheme – 我们需要使CustomSearchBar没有取消button,然后覆盖新的CustomSearchController中相应的属性:

 class CustomSearchBar: UISearchBar { override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) { super.setShowsCancelButton(false, animated: false) }} class CustomSearchController: UISearchController { lazy var _searchBar: CustomSearchBar = { [unowned self] in let customSearchBar = CustomSearchBar(frame: CGRect.zero) return customSearchBar }() override var searchBar: UISearchBar { get { return _searchBar } }} 

在MyViewController中,我使用这个新的自定义子类来初始化和configurationsearchController:

  var mySearchController: UISearchController = ({ // Display search results in a separate view controller // let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main) // let alternateController = storyBoard.instantiateViewController(withIdentifier: "aTV") as! AlternateTableViewController // let controller = UISearchController(searchResultsController: alternateController) let controller = CustomSearchController(searchResultsController: nil) controller.searchBar.placeholder = NSLocalizedString("Enter keyword (eg iceland)", comment: "") controller.hidesNavigationBarDuringPresentation = false controller.dimsBackgroundDuringPresentation = false controller.searchBar.searchBarStyle = .minimal controller.searchBar.sizeToFit() return controller })() 

我不得不通过一点点黑客来纠正

将viewDidLoad设置为0.0,因为他的屏幕会闪烁。

在你问… willPresentSearchController将无法正常工作。

 extension GDSearchTableViewController: UISearchControllerDelegate { func didPresentSearchController(searchController: UISearchController) { searchController.searchBar.setShowsCancelButton(false, animated: false) searchController.searchBar.becomeFirstResponder() UIView.animateWithDuration(0.1) { () -> Void in self.view.alpha = 1.0 searchController.searchBar.alpha = 1.0 } } } 

我还要补充一点

 searchController.hidesNavigationBarDuringPresentation = false searchController.delegate = self searchController.searchBar.delegate = self 

看看分配这些代表是否有帮助。

您可以inheritanceUISearchBar并覆盖方法layoutSubviews

 super.layoutSubviews() self.showsCancelButton = false 

我的解决scheme是每次重新设置属性,当我使用searchcontroller分别search栏。 我懒懒地初始化了searchcontroller而没有设置属性,然后做了

 searchController.searchBar.showsCancelButton = false 

每次search开始之前。 你可以在UISearchControllerDelegate方法中做到这一点,即..

我们希望search栏最初没有“取消”button,但是当用户在search栏中轻按时,它会显示。 然后我们想要取消button消失,如果用户点击取消,否则search栏丢失第一响应者。

什么终于为我工作:

在创造:

 searchBar.showsCancelButton = NO; 

我们使用UISearchBar的子类并重写searchBarShouldBeginEditing:

 -(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar { self.showsCancelButton = YES; return YES; } 

我们也覆盖了resignFirstReponder(在UISearchBar子类中):

 -(BOOL)resignFirstResponder { self.showsCancelButton = NO; return [super resignFirstResponder]; } 

这适用于我(iOS 10):

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.searchController.searchBar.showsCancelButton = NO; } 

那么用[searchBar setShowsCancelButton:NO animated:NO];设置它呢[searchBar setShowsCancelButton:NO animated:NO];

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/setShowsCancelButton:animated

我试图帮助你,但我不确定是否find真正的问题。

根据苹果文档 :

 showsCancelButton 

指示是否显示取消button的布尔属性

但是为了隐藏取消button,也许你应该使用:

 setShowsCancelButton(_:animated:) 

在这里输入图像说明

我希望能有所帮助。