在导航栏中使 SearchBar 更高
Posted
技术标签:
【中文标题】在导航栏中使 SearchBar 更高【英文标题】:Make SearchBar higher in navigation bar 【发布时间】:2018-02-06 21:48:07 【问题描述】:我尝试创建一个搜索栏,现在它工作正常,但我不知道如何让它更高一点,就在状态栏下方。 搜索栏是通过编程方式创建的,我使用了这段代码:
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad()
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
【问题讨论】:
【参考方案1】:您的意思是,您希望在导航栏的位置有一个搜索栏。如果是这种情况,请尝试在导航栏中添加搜索栏。
试试这个
var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
你保留了一个懒惰的 UISearchBar 属性
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))
在 viewDidLoad 中
searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
如果您想使用故事板,只需将您的搜索栏拖动为插座,然后将惰性属性替换为您的插座搜索栏
或者简单
// create the search bar programatically since you won't be
// able to drag one onto the navigation bar
searchBar = UISearchBar()
searchBar.sizeToFit()
// the UIViewController comes with a navigationItem property
// this will automatically be initialized for you if when the
// view controller is added to a navigation controller's stack
// you just need to set the titleView to be the search bar
navigationItem.titleView = searchBar
【讨论】:
是的,代码 navigationItem.titleView = searchBar 工作正常,搜索栏现在就在状态栏下方,谢谢。以上是关于在导航栏中使 SearchBar 更高的主要内容,如果未能解决你的问题,请参考以下文章
如何在 bootstrap 3 导航栏中使 select2 v4 搜索框响应?
Bootstrap 3:如何在导航栏中使下拉链接的头部可点击