在iOS 13中,可以通过以下步骤在navigationBar中隐藏UISearchController的searchBar:
以下是一个示例代码:
import UIKit
class ViewController: UIViewController, UISearchControllerDelegate {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
// 隐藏搜索栏
searchController.searchBar.isHidden = true
// 在搜索时隐藏导航栏
searchController.hidesNavigationBarDuringPresentation = true
// 在搜索时不模糊背景
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
}
extension ViewController: UISearchBarDelegate {
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
// 显示搜索栏
searchController.searchBar.isHidden = false
}
}
这样,当用户点击导航栏上的搜索按钮时,搜索栏将会显示出来,并且导航栏会自动隐藏。用户可以在搜索栏中输入关键字进行搜索。
领取专属 10元无门槛券
手把手带您无忧上云