在segue swift上显示活动的searchBar

时间:2017-01-10 17:03:04

标签: ios swift xcode

我有两个视图控制器。

带有按钮的视图控制器,以及带有UITableView和UISearchBar的视图控制器。

当用户点击第一个视图控制器中的按钮时,应用程序应该转到表格视图并立即显示一个活动的搜索栏,即。我不希望tableView显示自己,然后激活搜索栏,它需要处于活动状态,键入指示器在用户看到它时就已经闪烁。

当我点击应用顶部的“显示报价”搜索栏时,我想要的效果与Yahoo Finance应用程序中的效果相同。

尝试解决方案:

class myTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {

    var searchController: UISearchController!

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func viewWillAppear(_ animated: Bool) {
        configureSearchController()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        self.performSegue(withIdentifier: "unwind", sender: self)
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchController.searchBar.resignFirstResponder()
    }

    func configureSearchController() {
        // Initialize and perform a minimum configuration to the search controller.
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search here..."
        searchController.searchBar.delegate = self
        searchController.searchBar.sizeToFit()

        // Place the search bar view to the tableview headerview.
        tableView.tableHeaderView = searchController.searchBar

        searchController.delegate = self
        searchController.isActive = true
        searchController.definesPresentationContext = true
    }

    func didPresentSearchController(_ searchController: UISearchController) {
        searchController.searchBar.becomeFirstResponder()
    }
}

我的尝试解决方案仅在视图可见时激活搜索栏(用户仍然会看到搜索栏,暂时没有输入指示符)。

我不知道是否有一些方法可以使用prepareForSegue或任何其他方法。

非常感谢任何帮助!

0 个答案:

没有答案
相关问题