ios - 为什么在 searchBar 和 tableview 之间添加空白?

标签 ios swift uitableview uisearchcontroller

我有一个 TableView Controller 。我添加了搜索栏。但是当我点击 searchBar 时,我在 searchBar 和 TableView 之间有一个空白区域?为什么?它是如何修复的? 在下面,我添加了代码 tableViewController 的屏幕截图和列表。感谢您的帮助。

After click on searchBar

Click on searchBar

!!!!!! - 列出 TableViewController - !!!!!

class AlfavitController: UITableViewController, UISearchResultsUpdating {

var searchController : UISearchController!
var resultController = UITableViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    self.downloadData(param: Manager.id)

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.searchController = UISearchController(searchResultsController: self.resultController)
    self.tableView.tableHeaderView = self.searchController.searchBar
    self.searchController.searchResultsUpdater = self
    self.resultController.tableView.dataSource = self
    self.resultController.tableView.delegate = self
    definesPresentationContext = true
 }

func updateSearchResults(for searchController: UISearchController) {
    self.arFiltLet = self.arWords.filter{(lett : String) -> Bool in
        if lett.lowercased().contains(self.searchController.searchBar.text!.lowercased()){
            return true
        }else{
            return false
        }
    }
    self.resultController.tableView.reloadData()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.tableView {
        return self.arWords.count
    }else{
        return self.arFiltLet.count
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if tableView == self.tableView{
        cell.textLabel?.text = self.arWords[indexPath.row]
    }else{
        cell.textLabel?.text = self.arFiltLet[indexPath.row]
    }
    return cell
}

最佳答案

原因 发生这种情况的原因是 UIViewController 的属性 automaticallyAdjustsScrollViewInsets,这是一个 bool 值,指示 View Controller 是否应自动调整其 ScrollView 插入。默认为 YES。

解决方案:-

要么在你的 viewDidLoad() 中编写以下代码:

automaticallyAdjustsScrollViewInsets = false

或者通过你使用的 Storyboard/XIB 设置它:

enter image description here

关于ios - 为什么在 searchBar 和 tableview 之间添加空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41416567/

相关文章:

ios 应用程序 - 自定义应用程序的手动安装

ios - 更新大小未知的多行 UILabel 的 preferredMaxLayoutWidth(自动布局)

ios - 为什么 Xcode 模拟器 View 与 View 层次结构 View 不同

ios - PF LogInViewController Logo 扁平化

ios - 在没有 iTunes 的情况下访问 iOS 应用程序的共享文件

swift - 为 OpenWhisk/Kitura Swift HTTP POST 请求发送表单数据/应用程序/x-www-form-urlencoded 正文

swift - 确定 swift 对象是否具有父类(super class)

ios - 如何在表格 View 单元格中设置色调/突出显示颜色?

objective-c - didSelectRowAtIndexPath 一次选择多行

iphone - 如何以编程方式将 UIToolbar 添加到 UITableViewController?