gpt4 book ai didi

ios - 移除 UISearchController 和 UITableViewController 之间的空间

转载 作者:行者123 更新时间:2023-11-28 06:11:34 26 4
gpt4 key购买 nike

我有一个非常简单的 ViewController(嵌入在 NavigationController 中)。您可以从 NavigationBar 打开 UISearchController 来查找一些信息。搜索结果在一个非常简单的 TableViewController 中。

当显示 UISearchController 时,SearchController 和 TableViewController 之间有一些额外的空间。状态栏的高度好像是20px。

如何删除这个多余的空间?

查看屏幕截图。

enter image description here enter image description here

这里是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()
}
}

这里是TableViewController的代码

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 之间的空间问题。接下来还有一个问题,就是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)

}

关于ios - 移除 UISearchController 和 UITableViewController 之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46379091/

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