在 iOS 8 中的导航栏中显示搜索栏
Displaying search bar in navigation bar in iOS 8
UISearchDisplayController
有一个 boolean
属性 叫 displaysSearchBarInNavigationBar
。在 iOS 8 中有什么等价物可以让我的搜索栏向上移动?非常感谢任何指导。
这是我的代码,我不完全确定为什么它不起作用。当我单击搜索栏时,它只是消失了,而不是将其自身和导航栏向上移动。
import UIKit
class ViewController: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
let searchController = UISearchController(searchResultsController: nil)
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = true
tableView.dataSource = self
tableView.delegate = self
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
self.setupSearchBar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
func setupSearchBar() {
// Set search bar position and dimensions
var searchBarFrame: CGRect = self.searchController.searchBar.frame
var viewFrame = self.view.frame
self.searchController.searchBar.frame = CGRectMake(searchBarFrame.origin.x, searchBarFrame.origin.y + 64,viewFrame.size.width, 44)
// Add search controller's search bar to our view and bring it to forefront
self.view.addSubview(self.searchController.searchBar)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}
根据 Apple 的说法:
UISearchDisplayController
is deprecated in iOS 8. (Note that UISearchDisplayDelegate
is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController
.
The UISearchController
class defines an interface that manages the presentation of a search bar in concert with the search results controller’s content. The search results controller, a UIViewController
object specified by the searchResultsController property, manages the results of the search.
现在您可以使用 UISearchController
按以下方式在导航栏中显示搜索栏:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
但是你必须考虑到你需要设置一个 UINavigationController
就像在这个 Storyboard 中一样:
您只需 select 您的 ViewController
即可轻松完成,并查看以下步骤:
然后当您在搜索栏中单击时,您应该会在设备中看到以下图片:
希望对你有所帮助
UISearchDisplayController
有一个 boolean
属性 叫 displaysSearchBarInNavigationBar
。在 iOS 8 中有什么等价物可以让我的搜索栏向上移动?非常感谢任何指导。
这是我的代码,我不完全确定为什么它不起作用。当我单击搜索栏时,它只是消失了,而不是将其自身和导航栏向上移动。
import UIKit
class ViewController: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
let searchController = UISearchController(searchResultsController: nil)
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = true
tableView.dataSource = self
tableView.delegate = self
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
self.setupSearchBar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
func setupSearchBar() {
// Set search bar position and dimensions
var searchBarFrame: CGRect = self.searchController.searchBar.frame
var viewFrame = self.view.frame
self.searchController.searchBar.frame = CGRectMake(searchBarFrame.origin.x, searchBarFrame.origin.y + 64,viewFrame.size.width, 44)
// Add search controller's search bar to our view and bring it to forefront
self.view.addSubview(self.searchController.searchBar)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}
根据 Apple 的说法:
UISearchDisplayController
is deprecated in iOS 8. (Note thatUISearchDisplayDelegate
is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead useUISearchController
.The
UISearchController
class defines an interface that manages the presentation of a search bar in concert with the search results controller’s content. The search results controller, aUIViewController
object specified by the searchResultsController property, manages the results of the search.
现在您可以使用 UISearchController
按以下方式在导航栏中显示搜索栏:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
但是你必须考虑到你需要设置一个 UINavigationController
就像在这个 Storyboard 中一样:
您只需 select 您的 ViewController
即可轻松完成,并查看以下步骤:
然后当您在搜索栏中单击时,您应该会在设备中看到以下图片:
希望对你有所帮助