视图加载后搜索栏不显示-iOS

时间:2018-10-06 21:18:19

标签: swift uisearchcontroller

我的一种观点发起时,我遇到了问题。我试图使搜索栏在视图启动时显示,但是在我开始向下滚动时显示。 当我单击它时显示: enter image description here

,并且我试图在启动视图时显示它,该视图当前仅在我开始滚动时显示:

enter image description here

到目前为止,这是我必须设置搜索控制器的代码:

    searchController.searchBar.scopeButtonTitles = ["Posts", "Users"]
        searchController.searchBar.delegate = self
        navigationController?.navigationItem.searchController = searchController
        navigationController?.navigationItem.searchController?.searchBar.isHidden = false
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search"
        searchController.searchBar.isHidden = false
        searchController.searchBar.showsScopeBar = true
//        searchController.hidesNavigationBarDuringPresentation = false
        self.navigationController?.setNavigationBarHidden(false, animated: true)

        navigationItem.searchController = searchController
//        navigationController?.navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true

我尝试了许多不同的方法来使搜索栏在单击时显示,但是我没有成功。有任何想法吗?

1 个答案:

答案 0 :(得分:2)

您需要添加以下行:

navigationItem.hidesSearchBarWhenScrolling = false

如果您不想一开始就显示是否在滚动时将其隐藏,那么您需要执行以下操作:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = false
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = true
    }
}