导航栏中的中心搜索控制器

问题描述 投票:0回答:1

我想在导航栏中间添加一个搜索栏,如下所示:

enter image description here

我能够使用以下代码实现上述屏幕:

    navigationItem.titleView = searchController.searchBar
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.viewDelegate = self

但是,搜索栏无法访问。

我也尝试添加搜索控制器,并且它是可以访问的。触摸功能正常,我可以在其中输入文本:

    navigationItem.searchController = searchController
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.viewDelegate = self

但是输出是这样的,这不是我想要的:

enter image description here

以下是我尝试过的步骤:

  1. 在 viewWillAppear 中或加载视图控制器时设置 self.definesPresentationContext = false 。
  2. 检查是否有任何视图遮挡了该视图并将其设置为 false。

如何将搜索控制器置于导航栏中居中并使其同时可供用户访问

swift uikit uinavigationcontroller uinavigationbar uisearchcontroller
1个回答
0
投票
let searchBar = UISearchBar()
searchBar.placeholder = "Search for Products"
searchBar.delegate = self
searchBar.sizeToFit()

navigationItem.titleView = searchBar

尝试一下,如果它解决了问题,或者是否有变化,请告诉我。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.