By Mariam


2019-06-09 07:32:24 8 Comments

I need to add UISearchController to UIViewController I know that the best way to do it is adding it to UINavigationController like this:

    let resultSearchController = UISearchController(searchResultsController: athkarSearchTable)
    resultSearchController?.searchResultsUpdater = athkarSearchTable

    navigationItem.searchController = resultSearchController

The problem is I can't use UINavigationController so I found another solution: adding it to tableHeaderView like the following:

let resultSearchController = ({
        let controller = UISearchController(searchResultsController: athkarSearchTable)

        controller.searchResultsUpdater = athkarSearchTable
        // to give a semi-transparent background when the search bar is selected.
        controller.dimsBackgroundDuringPresentation = true
        controller.searchBar.sizeToFit()
        controller.searchBar.barStyle = UIBarStyle.default
        controller.searchBar.searchBarStyle = .minimal

        tableView.tableHeaderView = controller.searchBar

        return controller
    })()
    // to limit the overlap area to just the View Controller’s frame instead of the whole Navigation Controller
        definesPresentationContext = true

However, this solution adds an empty space to the bottom of the tableView in the UIViewController not the athkarSearchTable which is not good :/ as the screenshot below:

enter image description here

I tried to set the footer to an empty view and to set the height of footer to zero, none of them worked :/

Any suggestion how to remove the bottom empty space?

1 comments

@Mariam 2019-06-12 07:43:52

Problem solved by adding the following lines:

tableView.rowHeight          = UITableView.automaticDimension
tableView.estimatedRowHeight = 400

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How do I set the height of tableHeaderView (UITableView) with autolayout?

1 Answered Questions

[SOLVED] UITableView under transparent navigation bar

3 Answered Questions

[SOLVED] Add button in tableview footer

3 Answered Questions

[SOLVED] UISearchController with nil searchResultsController

1 Answered Questions

[SOLVED] Transparent space above 1st section in table view after rotate

1 Answered Questions

[SOLVED] How to change iOS "UITableView negative space" color?

1 Answered Questions

Sponsored Content