Swift结果未通过Storyboard添加SearchBar更新

时间:2017-12-13 14:34:46

标签: ios swift uisearchcontroller

我有一个完美的SearchController,目前位于tableView的顶部

tableView.tableHeaderView = searchController.searchBar

但是我想让searchBar始终保持在tableView之上,即使在滚动时也是如此。我通过故事板添加了它,但它显示但是它没有正常工作,当我在mySearch输入时,结果没有更新 我的代码:

@IBOutlet weak var mySearch: UISearchBar!

func configureSearchController(){
    searchController = UISearchController(searchResultsController: nil)
    definesPresentationContext = true
    searchController.definesPresentationContext = true
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search for songs"
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()
    searchController.searchBar.tintColor = GlobalSettings.tint.color
    searchController.searchBar.contentMode = .scaleAspectFill
    searchController.hidesNavigationBarDuringPresentation = false;
    searchController.searchBar.searchBarStyle = .minimal

    mySearch = searchController.searchBar
}

如何正确连接@IBOutlet searchBarsearchController

1 个答案:

答案 0 :(得分:0)

您是否尝试过将插座从UISearchBar更改为UISearchController? 如果我没弄错的话,它确实可以在故事板中使用UISearchBar。

这是更新的代码:

@IBOutlet weak var mySearch: UISearchController!

func configureSearchController(){
        definesPresentationContext = true
        searchController.definesPresentationContext = true
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search for songs"
        searchController.searchBar.delegate = self
        searchController.searchBar.sizeToFit()
        searchController.searchBar.tintColor = GlobalSettings.tint.color
        searchController.searchBar.contentMode = .scaleAspectFill
        searchController.hidesNavigationBarDuringPresentation = false;
        searchController.searchBar.searchBarStyle = .minimal
    }