将 UISearchBar 放在 tableview 上方但不在 tableview 或 navbar 中
Place UISearchBar above tableview but not in tableview or navbar
我的情节提要中的视图控制器上有一个 UISearchBar 和一个 UITableView。搜索栏位于 tableview 上方,因此它不会滚动。我的问题是 UISearchController 的初始化创建了它自己的 SearchBar 实例,该实例与我手动添加并定位在故事板上的实例不同。
如何将故事板 SearchBar 设置为 UISearchController 为我创建的那个?
class ITSearchController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
// So that the search results are displayed in the current controllers table view
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// This didn't work
//var view = self.view.viewWithTag(777)
//view = searchController.searchBar
// This works but isn't what I want
//tableView.tableHeaderView = searchController.searchBar
// This works but places it in the nav section. Not what I want.
// self.navigationItem.titleView = searchController.searchBar
// This is what I want but doesn't work. Basically make the search bar on my storyboard equal to the one created.
searchBar = searchController.searchBar
}
}
由于 UISearchController 提供了自己的故事板,因此您无法将放置在故事板上的搜索栏与 UISearchController
一起使用。
作为替代方案,将故事板中的搜索栏替换为 UIView
,然后将 UISearchController
提供的搜索栏添加为 UIView
的子视图。
class ITSearchController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchView: UIView! // make this UIView
// So that the search results are displayed in the current controllers table view
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// This should work
searchView.addSubview(searchController.searchBar)
}
}
这会给你同样的行为。
我的情节提要中的视图控制器上有一个 UISearchBar 和一个 UITableView。搜索栏位于 tableview 上方,因此它不会滚动。我的问题是 UISearchController 的初始化创建了它自己的 SearchBar 实例,该实例与我手动添加并定位在故事板上的实例不同。
如何将故事板 SearchBar 设置为 UISearchController 为我创建的那个?
class ITSearchController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
// So that the search results are displayed in the current controllers table view
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// This didn't work
//var view = self.view.viewWithTag(777)
//view = searchController.searchBar
// This works but isn't what I want
//tableView.tableHeaderView = searchController.searchBar
// This works but places it in the nav section. Not what I want.
// self.navigationItem.titleView = searchController.searchBar
// This is what I want but doesn't work. Basically make the search bar on my storyboard equal to the one created.
searchBar = searchController.searchBar
}
}
由于 UISearchController 提供了自己的故事板,因此您无法将放置在故事板上的搜索栏与 UISearchController
一起使用。
作为替代方案,将故事板中的搜索栏替换为 UIView
,然后将 UISearchController
提供的搜索栏添加为 UIView
的子视图。
class ITSearchController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchView: UIView! // make this UIView
// So that the search results are displayed in the current controllers table view
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// This should work
searchView.addSubview(searchController.searchBar)
}
}
这会给你同样的行为。