【问题标题】:How to add scope buttons in a UISearchController embedded in UINavigationController如何在嵌入 UINavigationController 的 UISearchController 中添加范围按钮
【发布时间】:2016-04-24 13:07:52
【问题描述】:

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

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

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

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

接下来,当我点击取消按钮返回主视图控制器时,UISearchController 并没有恢复其原始样式

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

这是我在主视图控制器中添加 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 子类中添加了范围按钮,代码如下

    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 的背景

你能告诉我我做错了什么,我应该怎么做才能在 UISearchController 中正确集成 Scope Button? 我找到了示例,但仅当 UISearchController 未嵌入 UINavigationController 时。

感谢您的帮助!

塞巴斯蒂安。

【问题讨论】:

    标签: ios uinavigationcontroller uinavigationbar uisearchcontroller


    【解决方案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 中显示或隐藏您的范围按钮。这是通过设置:searchController.searchBar.showsScopeBar = true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      相关资源
      最近更新 更多