如何在embedded在UINavigationController中的UISearchController中添加范围button

我有一个应用程序呈现embedded在UINavigationController中的MKMapView。 在UINavigationController我已经把一个UISearchController。 当用户触摸UISearchController时,它显示一个UITableViewController。 它工作正常,而我不在UISearchController中添加范围button。

这里是我启动App时UINavigationController中UISearchController的屏幕截图。

在这里输入图像说明

接下来,当我触摸UISearchController时,它将显示UITableViewController和scopebutton。

在这里输入图像说明

在这里,我们已经可以看到这个范围button有一个问题,因为它们没有很好地集成在UISearchController中(颜色应该是半透明的)

接下来,当我触摸取消button返回到主视图控制器,UISearchController不恢复其原始样式

在这里输入图像说明

它有一个深灰色的边框(可能来自范围button)。

以下是我在主视图控制器中添加UISearchController的方法

func initSearchController() { let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController self.searchController = UISearchController(searchResultsController: mySearchController) mySearchController.theSearchController = self.searchController mySearchController.delegate = self // Configure the UISearchController self.searchController.searchResultsUpdater = self self.searchController.delegate = self self.searchController.searchBar.delegate = self self.searchController.searchBar.placeholder = "data.." self.searchController.hidesNavigationBarDuringPresentation = false self.searchController.dimsBackgroundDuringPresentation = true self.navigationItem.titleView = searchController.searchBar self.definesPresentationContext = true } 

这个方法在我的主视图控制器的viewDidLoad()中被调用。

接下来,当显示SearchController时,我在TableViewController子类中添加了以下代码的范围button

  override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // Mandatory to make sure the TableView is displayed when the search field is empty // when user touch it. view.hidden = false var rect = delegate.searchController.searchBar.superview?.frame rect?.size.height = 88 self.delegate.searchController.searchBar.scopeButtonTitles = ["one", "two", "three"] self.delegate.searchController.searchBar.showsScopeBar = true self.delegate.searchController.searchBar.superview?.frame = rect! } 

并且searchclosures时执行以下代码

  override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) var rect = delegate.searchController.searchBar.superview?.frame rect?.size.height = 44 self.delegate.searchController.searchBar.superview?.frame = rect! self.delegate.searchController.searchBar.showsScopeBar = false self.delegate.searchController.searchBar.scopeButtonTitles = nil } 

正如你可以看到我有这个代码几个问题。

  1. 范围button显示不正确,我无法添加一个很好的animation
  2. 当用户退出search范围button被删除,但它会影响UISearchController的背景

你能告诉我我做错了什么,我该做什么才能正确集成范围button在UISearchController ?. 我已经find了例子,但只有当UISearchController没有embeddedUINavigationController。

谢谢你的帮助!

塞巴斯蒂安。

你应该尝试在你的UISearchController实例中使用searchBar.scopeButtonTitles:

 func initSearchController() { let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController searchController = UISearchController(searchResultsController: mySearchController) // Set Scope Bar Buttons searchController.searchBar.scopeButtonTitles = ["one", "two", "three"] // searchController.searchBar.showsScopeBar = true //if you want it always visible // Configure the UISearchController searchController.searchResultsUpdater = self searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar searchController.delegate = self searchController.searchBar.delegate = self searchController.searchBar.placeholder = "data.." searchController.hidesNavigationBarDuringPresentation = false searchController.dimsBackgroundDuringPresentation = true definesPresentationContext = true } 

不需要在willAppear / didDisapear中显示或隐藏scopeButton。 这是由设置:searchController.searchBar.showsScopeBar = true