为什么在 searchBar 和 tableview 之间添加空格? [英] why added blank space between searchBar and tableview?

查看:22
本文介绍了为什么在 searchBar 和 tableview 之间添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tableview 控制器.我添加了搜索栏.但是当我单击searchBar 时,searchBar 和TableView 之间有一个空格?为什么?以及它是如何修复的?在下面,我添加了代码 tableViewController 的屏幕截图和列表.感谢您的帮助.

I have a tableview controller. I added searchBar. But when i click on searchBar, i have a blank space between searchBar and TableView? Why? And how it fix? In down i add screenshot and listing of code tableViewController. Thanks for help.

!!!!!!- 列表 TableViewController - !!!!!!

!!!!!! - LISTING 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 的属性 automaticAdjustsScrollViewInsets,这是一个布尔值,指示视图控制器是否应该自动调整其滚动视图插入.,默认为 YES.

The reason this happens is UIViewController’s property automaticallyAdjustsScrollViewInsets, which is a Boolean value that indicates whether the view controller should automatically adjust its scroll view insets., and defaults to YES.

解决方案:-

在您的 viewDidLoad() 中编写以下代码:

Either write below code in your viewDidLoad():

automaticallyAdjustsScrollViewInsets = false

或者通过故事板/XIB 设置它,无论您使用哪种方式:

Or set it through storyboard/XIB whichever you use:

这篇关于为什么在 searchBar 和 tableview 之间添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆