gpt4 book ai didi

ios - swift 4 : UISearchBar and NavigationBar

转载 作者:行者123 更新时间:2023-11-30 11:21:17 25 4
gpt4 key购买 nike

我正在尝试以编程方式在 UINavigationBar 中添加 UISearchBar

在我的 Storyboard中,我拖放 UINavigationBarUITableView

问题是,当我运行应用程序并下拉tableview时,SearchBar没有显示。

这是我的代码,

let searchController = UISearchController(searchResultsController: nil)

// MARK: - View Setup
override func viewDidLoad() {
super.viewDidLoad()

// Setup the Search Controller
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search "
navigationItem.searchController = searchController
definesPresentationContext = true


searchController.searchBar.delegate = self

使其工作的唯一方法是嵌入导航 Controller 。在没有导航 Controller 的情况下如何使其工作?

我用下面的建议更新了我的帖子,正如您所看到的,搜索栏在滚动时无法隐藏,甚至“HI”标签和按钮都消失了

enter image description here

    searchController.obscuresBackgroundDuringPresentation = true
searchController.searchBar.placeholder = "Search"
let item = UINavigationItem.init()
item.titleView = searchController.searchBar
item.hidesSearchBarWhenScrolling = true
self.NavigationBAr.setItems([item], animated: true)
definesPresentationContext = true
searchController.searchBar.delegate = self

最佳答案

试试这个

        let item = UINavigationItem.init()
item.titleView = searchController
self.navigationbar.setItems([item], animated: true)

关于ios - swift 4 : UISearchBar and NavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com