如何在UINavigationController

时间:2016-04-24 13:07:53

标签: ios uinavigationcontroller uinavigationbar uisearchcontroller

我有一个应用程序,它呈现嵌入在UINavigationController中的MKMapView。在UINavigationController中我放了一个UISearchController。当用户触摸UISearchController时,它会显示一个UITableViewController。 当我没有在UISearchController中添加Scope按钮时,它运行良好。

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

enter image description here

接下来,当我触摸UISearchController时,它会显示UITableViewController和范围按钮。

enter image description here

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

接下来,当我触摸取消按钮返回主viewController时,UISearchController没有恢复其原始样式

enter image description here

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

以下是我在主视图控制器

中添加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


}

在我的主ViewController的viewDidLoad()中调用此方法。

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

    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!
}

并在搜索关闭时执行以下代码

    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. 范围按钮显示不正确,我无法使用漂亮的动画添加它们
  2. 当用户退出搜索范围按钮被删除但它会影响UISearchController的背景
  3. 你能告诉我我做错了什么,我该怎么做才能在UISearchController中正确地集成Scope Button? 我找到了示例,但只有当UISearchController未嵌入UINavigationController时才会发现。

    感谢您的帮助!

    塞巴斯蒂安。

1 个答案:

答案 0 :(得分:1)

您应该尝试在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中显示或隐藏scopeButtons。这是由:searchController.searchBar.showsScopeBar = true

设置的