如何在嵌入 UINavigationController 的 UISearchController 中添加范围按钮 [英] How to add scope buttons in a UISearchController embedded in UINavigationController

查看:15
本文介绍了如何在嵌入 UINavigationController 的 UISearchController 中添加范围按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an App that is presenting a MKMapView embedded in a UINavigationController. In the UINavigationController I have put a UISearchController. When the User touch the UISearchController it displays a UITableViewController. It works well while I'm not adding the Scope button in the UISearchController.

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

Here the screenshot of the UISearchController in the UINavigationController when I start the App.

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

Next when I touch the UISearchController, it displays the UITableViewController and scope button.

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

Here we can already see there's an issue with the scope button because they are not well integrated in the UISearchController (color should be translucent)

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

Next, when I touch the Cancel button to go back to the Main viewController, the UISearchController is not recovering its original style

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

it has a dark gray border (that probably comes from the scope button).

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

Here's how I add the UISearchController in the Main view Controller

    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() 中调用此方法.

this method is called in the viewDidLoad() of my Main ViewController.

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

Next, when the SearchController is displayed, I'm adding the scope button with the following code in my TableViewController subclass

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

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

and the following code is executed when search is closed

    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
}

如您所见,我对这段代码有几个问题.

As you can see I have severals issues with this code.

  1. 范围按钮显示不正确,我无法添加漂亮的动画
  2. 当用户退出时,搜索范围按钮会被移除,但会影响 UISearchController 的背景

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

Can you tell me what I'm doing wrong and what should I do to integrate correctly Scope Button in UISearchController?. I have found examples but only when the UISearchController is not embedded in the UINavigationController.

感谢您的帮助!

塞巴斯蒂安.

推荐答案

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

You should try using the searchBar.scopeButtonTitles in your instance of UISearchController:

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

No need to show or hide your scopeButtons in willAppear/didDisapear. This is set by: searchController.searchBar.showsScopeBar = true

这篇关于如何在嵌入 UINavigationController 的 UISearchController 中添加范围按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆