繁体   English   中英

隐藏UISearchController swift 2.0上的取消按钮

[英]Hide Cancel Button on UISearchController swift 2.0

有什么办法可以隐藏UISearchController上的“取消”按钮吗? 在此处输入图片说明

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

谢谢

使用seachController.searchBar.setShowsCancelButton(false,animated:false)似乎仍然不能做到这一点。

尝试将UISearchBarDelegate添加到您的控制器,并将searchBar的委托分配给self。 然后,您可以使用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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM