gpt4 book ai didi

ios8 - 在iOS 8的导航栏中显示搜索栏

转载 作者:行者123 更新时间:2023-12-03 08:12:46 25 4
gpt4 key购买 nike

UISearchDisplayController具有一个称为booleandisplaysSearchBarInNavigationBar属性。在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
}
}

最佳答案

根据苹果的说法:

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.
}
}
但是您必须考虑需要在此Storyboard中设置一个 UINavigationController:

您只需选择 ViewController并查看以下步骤,即可轻松完成:

然后,当您在搜索栏中单击以下图片时,您应该在设备中看到以下图片:

希望对您有帮助

关于ios8 - 在iOS 8的导航栏中显示搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30226835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com