3

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.

After click on searchBar

Click on 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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
maxim rad
  • 67
  • 8
  • Possible duplicate of [Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7](http://stackoverflow.com/questions/18880341/why-is-there-extra-padding-at-the-top-of-my-uitableview-with-style-uitableviewst) – Adeel Miraj Jan 01 '17 at 16:59
  • Hello, I am facing same issue can any one get the solution of this ? – Yogesh Patel Sep 11 '19 at 09:49

1 Answers1

0

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:

enter image description here

Sagar Thummar
  • 2,034
  • 15
  • 21
Alex Posplaw
  • 116
  • 8