删除 UISearchController 和 UITableViewController 之间的 space
Remove space between UISearchController and UITableViewController
我有一个非常简单的 ViewController(嵌入在 NavigationController 中)。您可以从 NavigationBar 打开 UISearchController 来查找一些信息。
搜索结果在一个非常简单的表ViewController.
中
当显示 UISearchController 时,SearchController 和 TableViewController 之间有一些额外的 space。状态栏的高度好像是20px。
如何删除这个多余的 space?
查看屏幕截图。
这里是ViewController
的代码
class ViewController: UIViewController {
var theSearchController:UISearchController?
@IBAction func openSearchController(_ sender: UIBarButtonItem) {
showSearchController()
}
/// Display the search controller used to look for a POI, address, ...
func showSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchControllerId") as! SearchTableViewController
theSearchController = UISearchController(searchResultsController: mySearchController)
// Configure the UISearchController
theSearchController!.searchResultsUpdater = self
theSearchController!.delegate = self
theSearchController!.searchBar.sizeToFit()
theSearchController!.searchBar.delegate = self
theSearchController!.searchBar.placeholder = NSLocalizedString("MapSearchPlaceHolder", comment: "")
theSearchController!.hidesNavigationBarDuringPresentation = true
theSearchController!.dimsBackgroundDuringPresentation = true
present(theSearchController!, animated: true, completion: nil)
}
}
extension ViewController: UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {
let mySearchController = searchController.searchResultsController as! SearchTableViewController
mySearchController.reloadAll()
}
}
这里是表的代码ViewController
class SearchTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.isHidden = false
}
func reloadAll() {
view.isHidden = false
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)
cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"
return cell
}
}
编辑:
Rizvan Rzayev 的回答解决了 UISearchController 和 UITableviewController 之间的 space 问题。
接下来还有一个问题,就是tableview的底部被键盘遮住了,导致有些行无法显示
解决这个键盘问题和初始问题的完整代码:
import UIKit
class SearchTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
registerKeyboardNotifications()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.isHidden = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
deinit {
unregisterKeyboardNotifications()
}
func reloadAll() {
view.isHidden = false
tableView.reloadData()
}
// MARK: - Table view data source
func unregisterKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(SearchTableViewController.keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(SearchTableViewController.keyboardWillHide(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
var keyboardHeight:CGFloat = 0.0
@objc func keyboardWillShow(_ notification:Notification) {
// Extract the Keyboard size
let info = notification.userInfo! as NSDictionary
let valueHeight = info.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardForHeight = valueHeight.cgRectValue.size
keyboardHeight = keyboardForHeight.height
let contentInsets = UIEdgeInsetsMake(40, 0.0, keyboardForHeight.height, 0.0)
tableView.contentInset = contentInsets
tableView.scrollIndicatorInsets = contentInsets
}
@objc func keyboardWillHide(_ notification:Notification) {
tableView.contentInset = .zero
tableView.scrollIndicatorInsets = .zero
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)
cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: keyboardHeight, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset
}
}
试试吧!
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.contentInset = UIEdgeInsets(top: 65, left: 0, bottom: 0, right: 0)
}
我有一个非常简单的 ViewController(嵌入在 NavigationController 中)。您可以从 NavigationBar 打开 UISearchController 来查找一些信息。 搜索结果在一个非常简单的表ViewController.
中当显示 UISearchController 时,SearchController 和 TableViewController 之间有一些额外的 space。状态栏的高度好像是20px。
如何删除这个多余的 space?
查看屏幕截图。
这里是ViewController
的代码class ViewController: UIViewController {
var theSearchController:UISearchController?
@IBAction func openSearchController(_ sender: UIBarButtonItem) {
showSearchController()
}
/// Display the search controller used to look for a POI, address, ...
func showSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchControllerId") as! SearchTableViewController
theSearchController = UISearchController(searchResultsController: mySearchController)
// Configure the UISearchController
theSearchController!.searchResultsUpdater = self
theSearchController!.delegate = self
theSearchController!.searchBar.sizeToFit()
theSearchController!.searchBar.delegate = self
theSearchController!.searchBar.placeholder = NSLocalizedString("MapSearchPlaceHolder", comment: "")
theSearchController!.hidesNavigationBarDuringPresentation = true
theSearchController!.dimsBackgroundDuringPresentation = true
present(theSearchController!, animated: true, completion: nil)
}
}
extension ViewController: UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {
let mySearchController = searchController.searchResultsController as! SearchTableViewController
mySearchController.reloadAll()
}
}
这里是表的代码ViewController
class SearchTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.isHidden = false
}
func reloadAll() {
view.isHidden = false
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)
cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"
return cell
}
}
编辑:
Rizvan Rzayev 的回答解决了 UISearchController 和 UITableviewController 之间的 space 问题。 接下来还有一个问题,就是tableview的底部被键盘遮住了,导致有些行无法显示
解决这个键盘问题和初始问题的完整代码:
import UIKit
class SearchTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
registerKeyboardNotifications()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.isHidden = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
deinit {
unregisterKeyboardNotifications()
}
func reloadAll() {
view.isHidden = false
tableView.reloadData()
}
// MARK: - Table view data source
func unregisterKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(SearchTableViewController.keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(SearchTableViewController.keyboardWillHide(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
var keyboardHeight:CGFloat = 0.0
@objc func keyboardWillShow(_ notification:Notification) {
// Extract the Keyboard size
let info = notification.userInfo! as NSDictionary
let valueHeight = info.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardForHeight = valueHeight.cgRectValue.size
keyboardHeight = keyboardForHeight.height
let contentInsets = UIEdgeInsetsMake(40, 0.0, keyboardForHeight.height, 0.0)
tableView.contentInset = contentInsets
tableView.scrollIndicatorInsets = contentInsets
}
@objc func keyboardWillHide(_ notification:Notification) {
tableView.contentInset = .zero
tableView.scrollIndicatorInsets = .zero
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)
cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: keyboardHeight, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset
}
}
试试吧!
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.contentInset = UIEdgeInsets(top: 65, left: 0, bottom: 0, right: 0)
}