使 SearchBar 在导航栏中更高
Make SearchBar higher in navigation bar
我尝试创建一个搜索栏,它现在工作正常,但我不知道如何让它稍微高一点,就在状态栏下面。
搜索栏是以编程方式创建的,我使用了这段代码:
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
}
你的意思是,你想要一个搜索栏放在导航栏的地方。如果是这种情况,请尝试在导航栏中添加搜索栏。
试试这个
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
我尝试创建一个搜索栏,它现在工作正常,但我不知道如何让它稍微高一点,就在状态栏下面。 搜索栏是以编程方式创建的,我使用了这段代码:
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
}
你的意思是,你想要一个搜索栏放在导航栏的地方。如果是这种情况,请尝试在导航栏中添加搜索栏。
试试这个
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