ios 11搜索栏不显示navigationItem

时间:2018-04-07 05:25:54

标签: search navigation swift4 searchbar navigationcontroller

我遇到了一个问题,无论我做什么,我的搜索栏(嵌入在表视图中)都不显示。当我实现iOS 11 navigationItem代码时,它就不存在了。搜索控制器代码如下。谢谢!

func configureSearchBar() {            

            resultSearchController = UISearchController(searchResultsController: nil)

            if #available(iOS 9.0, *) {
                resultSearchController.loadViewIfNeeded()
            } else {
                let _ = self.resultSearchController.view
            }

            let searchField = resultSearchController.searchBar.value(forKey: "searchField") as! UITextField
            searchField.font = DifferentScreens.searchField()
            resultSearchController.searchResultsUpdater = self
            resultSearchController.dimsBackgroundDuringPresentation = false
            resultSearchController.obscuresBackgroundDuringPresentation = false
            resultSearchController.searchBar.placeholder = "Search Gifts"

            if #available(iOS 11.0, *) {

                navigationItem.searchController = resultSearchController
                navigationItem.hidesSearchBarWhenScrolling = false
            } else {

                giftTableView.tableHeaderView = resultSearchController.searchBar
            }

            resultSearchController.searchBar.delegate = self
            resultSearchController.searchBar.tintColor = UIColor.black
            resultSearchController.searchBar.barTintColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)
            definesPresentationContext = false
        }

1 个答案:

答案 0 :(得分:0)

我已经决定从界面构建器实现UISearchBar。它似乎工作正常。