UISearchBar 高度自动改变大小 Swift
UISearchBar Height Automatic Size Change Swift
这是我正在尝试做的事情:
截图来自Iphone:
我正在开发一个简单的应用程序并以编程方式添加 uisearchbar 但我真的很困惑为什么要像第二张图片一样从顶部显示额外的 space 。但是当我写的时候,uisearchbar 改变了我们的位置,就像第一张图片一样。
这是我的代码:
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Search"
self.searchController.searchBar.barStyle = .black
self.searchController.searchBar.delegate = self
self.definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
}
}
extension starControl: UISearchBarDelegate{
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
{
//Show Cancel
searchBar.setShowsCancelButton(true, animated: true)
searchBar.tintColor = .black
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchBar.setShowsCancelButton(false, animated: true)
searchBar.resignFirstResponder()
guard let term = searchBar.text , term.isEmpty == false else {
return
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
searchBar.setShowsCancelButton(false, animated: true)
searchBar.text = String()
searchBar.resignFirstResponder()
}
}
谁能告诉我如何解决这个问题,我已经尝试解决这个问题但还没有结果。
如有任何帮助,我们将不胜感激。
提前致谢。
这是最新的 swift4.2 代码和搜索栏的最新功能只是将此功能放在控制器中并从 viewDidLoad 调用。
func setupNavBar() {
self.title = "Controller title"
self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
}
这是我正在尝试做的事情:
截图来自Iphone:
我正在开发一个简单的应用程序并以编程方式添加 uisearchbar 但我真的很困惑为什么要像第二张图片一样从顶部显示额外的 space 。但是当我写的时候,uisearchbar 改变了我们的位置,就像第一张图片一样。
这是我的代码:
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Search"
self.searchController.searchBar.barStyle = .black
self.searchController.searchBar.delegate = self
self.definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
}
}
extension starControl: UISearchBarDelegate{
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
{
//Show Cancel
searchBar.setShowsCancelButton(true, animated: true)
searchBar.tintColor = .black
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchBar.setShowsCancelButton(false, animated: true)
searchBar.resignFirstResponder()
guard let term = searchBar.text , term.isEmpty == false else {
return
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
searchBar.setShowsCancelButton(false, animated: true)
searchBar.text = String()
searchBar.resignFirstResponder()
}
}
谁能告诉我如何解决这个问题,我已经尝试解决这个问题但还没有结果。
如有任何帮助,我们将不胜感激。
提前致谢。
这是最新的 swift4.2 代码和搜索栏的最新功能只是将此功能放在控制器中并从 viewDidLoad 调用。
func setupNavBar() {
self.title = "Controller title"
self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
}