【问题标题】:Hide Cancel Button on UISearchController swift 2.0隐藏 UISearchController swift 2.0 上的取消按钮
【发布时间】:2015-11-06 20:37:23
【问题描述】:

有什么方法可以隐藏 UISearchController 上的取消按钮?

我想知道它是否可能的另一个行为是当用户按下取消按钮时,在 UISearchBar 上设置文本。

谢谢

【问题讨论】:

  • 按钮类有隐藏方法吗?
  • @Lamour " self.searchController.searchBar.setShowsCancelButton(false, animated: false) " 在 UISearchController 初始化时,不起作用
  • 试试this answer
  • @pbasdf 这可行,但取消按钮会在消失之前短暂显示。
  • 您可以尝试该问题的其他答案 - 尽管我承认这是一个混乱的解决方案。

标签: ios swift search ios9 uisearchbar


【解决方案1】:

使用 seachController.searchBar.setShowsCancelButton(false, animated:false) 似乎仍然没有这样做。

尝试将 UISearchBarDelegate 添加到您的控制器并将 searchBar 的委托分配给自己。然后您可以使用searchBarShouldBeginEditing 来编辑其可见性。

    import UIKit

    class TableViewController: UITableViewController, UISearchBarDelegate {

    var searchController: UISearchController!
    let menuItems: [String] = ["Row 1", "Row 2", "Row 3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultsViewControllerStoryboardIdentifier") as! SearchResultsTableViewController


        searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.delegate = self
    }

    @IBAction func searchButtonPressed(sender: UIBarButtonItem) {
        presentViewController(searchController, animated: true, completion: nil)
    }

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        searchController.searchBar.setShowsCancelButton(false, animated: true)

        return true
    }

    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuItems.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        cell.textLabel?.text = self.menuItems[indexPath.row]

        return cell
    }
}

【讨论】:

  • 如果你可以发布你的代码,看看问题出在哪里会很有帮助。我可以用上面的代码创建一个简单的应用程序。我已经用完整的 TableViewController 代码更新了代码。
猜你喜欢
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多