0

This is how I setup my UISearchController (I am using a new controller BackupSearchResultsVC to show the search result)

class BackupViewViewController: UIViewController {

    private lazy var searchController: UISearchController = {
        let backupSearchResultsVC = BackupSearchResultsVC.instanceFromNib()
        backupSearchResultsVC.postInit(nsBackup)
        
        let searchController = UISearchController(searchResultsController: backupSearchResultsVC)
        
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.searchBar.placeholder = "search_notes".localized
        searchController.searchBar.delegate = self
        
        return searchController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /** Specify that this view controller determines how the search controller is presented.
            The search controller should be presented modally and match the physical size of this view controller.
        */
        definesPresentationContext = true
        
        navigationItem.searchController = searchController
    }

This is the UI before search begins.

enter image description here

Once the search begins (The search bar will move upward to block the title bar when the search text field is in focus), there is a wide gap between the search result (red background) and the search text bar.

enter image description here

Is there any way to avoid the gap, yet able to use a new controller to show the result?

Thanks.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

0

By default, the presentation is full screen and the searchResultsController goes under the nav bar which has the search bar.

I believe this has something to do with the autolayout constraints / background color set up of your view controller set up to be the searchResultsController

For example, I have set up the like this:

class BackupSearchResultsVC: UIViewController, UISearchResultsUpdating
{
    let resultsView = UIView()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        configureResultsView()
    }
    
    private func configureResultsView()
    {
        resultsView.translatesAutoresizingMaskIntoConstraints = false
        resultsView.backgroundColor = .red
        view.addSubview(resultsView)
        
        // Add constraints with top anchor of 200
        view.addConstraints([
            
            resultsView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            resultsView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
            resultsView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            resultsView.trailingAnchor.constraint(equalTo: view.trailingAnchor)

        ])
    }

Custom UIViewController for SearchResultsController not full screen swift iOS

Because of the top anchor, it gives me the same results you see:

If you want to keep the red view's position as it is, add a background color to your view controller's main view

override func viewDidLoad()
{
    super.viewDidLoad()
    
    // Give a background color
    view.backgroundColor = .white
    
    configureResultsView()
}

UISearchController with custom UIViewController as search results controller swift iOS

Or you could just remove the constraint to the top

// Add constraints without top anchor of 200
view.addConstraints([
    
    resultsView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    resultsView.topAnchor.constraint(equalTo: view.topAnchor),
    resultsView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    resultsView.trailingAnchor.constraint(equalTo: view.trailingAnchor)

])

Custom UIViewController for UISearchController swift iOS

If this still does not work, try backupSearchResultsVC.modalPresentationStyle = .fullScreen during your set up

Give one of these a go and see if it solves your problem

Update based on Mr Cheng's

It seems giving the UIViewController used as the searchResultsController seems to make the UINavigationBar go transparent when active and the full view controller is shown behind:

Custom UIViewController as search results controller UISearchController iOS Swift

This is different to what I see running the same code:

Custom UIViewController for UISearchController search results iOS Swift

This leads me to believe this is iOS related as my device is iOS 14 and the result you see is probably iOS 15.

Digging a bit deeper, I came across this post and this which suggests by default, the bar tint is transparent on iOS 15.

So I believe you might need to set a custom background color for the nav bar in iOS 15 as outlined here perhaps

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
  • Hi, I tend to produce a simple example - https://github.com/yccheok/SearchBarIssue But I am still not able to achieve your outcome. Do you have idea why? Thank you for your help! – Cheok Yan Cheng Mar 01 '22 at 05:19
  • Hello @CheokYanCheng, I think this could be iOS related. Please see my updated answer if it helps you. At the moment I cannot test iOS 15, however I added some suggestions which might help. – Shawn Frank Mar 01 '22 at 06:25