Search bar adds extra padding on clicking it

1.2k Views Asked by At

When I click on my search bar I get extra padding below search bar. I do have custom height set, but even if I try to take back to the default one it is giving me same problems.I added the code below now. I don't have any search bar in the storyboard. I create it with the code below enter image description here

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {
    var modelData: NSMutableArray = []
    var filteredData: NSMutableArray = []
    var resultSearchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()


        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()
    // Reload the table b
    tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // 1
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // 2
    if (self.resultSearchController.active) {
        return self.filteredData.count
    }
    else {
        return self.modelData.count
    }
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("detailLabels") as! TableViewCell

    // 3
    if (self.resultSearchController.active) {
        let data = self.filteredData[indexPath.row] as? [String: String]
        cell.newNameLabel?.text = data?["Name"]
        cell.newSizeLabel?.text = data?["Size"]
        cell.newPriceLabel?.text = data?["Price"]
        return cell
    }
    else {
        let data = self.modelData[indexPath.row] as? [String: String]
        cell.nameLabel?.text = data?["Name"]
        cell.sizeLabel?.text = data?["Size"]
        cell.priceLabel?.text = data?["Price"]
        return cell
    }
}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filteredData.removeAllObjects()

    let searchPredicate = NSPredicate(format: "Name CONTAINS %@", searchController.searchBar.text)
    let array = (modelData as NSArray).filteredArrayUsingPredicate(searchPredicate)
    println(array)
    filteredData.addObjectsFromArray(array)

    tableView.reloadData()
}
2

There are 2 best solutions below

0
On

It's very likely that your tableview controller is automatically adjusting scrollview insets, assuming that the tableview is extended under the search bar. It looks like you don't extend your tableview underneath the search bar so the tableview controller shouldn't automatically adjust the insets.

Try adding the following line inside of viewDidLoad.

self.automaticallyAdjustsScrollViewInsets = false

This prevents the tableview controller from adjusting the scrollview insets and should solve your bug.

1
On

Add the following code into getcell Override method from data source to fix this issue:

tableView.ContentInset = new UIEdgeInsets(45, 0, 0, 0);