gpt4 book ai didi

swift - UINavigationBar 中的 searchBar isHidden 后获取标题

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

我有一个 searchBar,当按下 searchButton 时,它会显示在 UINavigationBar 中。搜索完成或用户取消搜索后,我希望 title 返回到 UINavigationBar 我怎样才能将其返回?

搜索栏显示如下:

@IBAction func searchButtonTapped(_ sender: Any) {

if searchBar.isHidden == true {
searchBar.isHidden = false
self.navigationItem.titleView = self.searchBar
searchActive = true
} else if searchBar.isHidden == false {
searchBar.isHidden = true
searchActive = false
//Show title again
}
}

最佳答案

只要这样做,然后就不会弄乱 View Controller 的设置标题方法

@IBAction func searchButtonTapped(_ sender: Any) {
if searchBar.isHidden == true {
searchBar.isHidden = false
self.navigationItem.titleView = self.searchBar
searchActive = true
} else if searchBar.isHidden == false {
searchBar.isHidden = true
searchActive = false
let label = UILabel()
label.text = "My Title"
label.textColor = .red
self.navigationItem.titleView = label
}
}

关于swift - UINavigationBar 中的 searchBar isHidden 后获取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58611998/

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