简体   繁体   中英

why added blank space between searchBar and tableview?

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.

点击searchBar后

点击searchBar

!!!!!! - 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
}

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.

Solution:-

Either write below code in your viewDidLoad() :

automaticallyAdjustsScrollViewInsets = false

Or set it through storyboard/XIB whichever you use:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM