How to remove iOS 11 search bar from view and update UI
Asked Answered
T

2

10

In xcode 9 when I put the search bar in view its moves the rest of the UI down automatically. When I try to remove the searchbar from view I get a black space instead of the UI being corrected. The line I am using is: self.navigationItem.searchController = nil. I tried many things but I do not know how to correct the UI after the search bar is removed. Only when I move to another view controller and back the UI is correct again without the search bar. What am I missing here?

code:

@IBAction func searchIconPressed(_ sender: UIBarButtonItem) {

    //ios 11
    if #available(iOS 11.0, *) {
         if self.navigationItem.searchController == nil {
            self.navigationItem.searchController = self.searchController
            self.searchController.isActive = true

        }
        else {
            self.navigationItem.searchController?.isActive = false
            self.navigationItem.searchController = nil
        }
    }
    //when ios 9-10
    else {
        if self.navigationItem.titleView == nil {
            self.navigationItem.titleView = self.searchController.searchBar
            self.searchController.isActive = true
        }
        else {
            self.navigationItem.titleView = nil
        }
    }
}

}

Triceps answered 1/10, 2017 at 15:55 Comment(0)
T
6

The answer for removing the search bar from view and make UI move up again:

self.navigationItem.searchController?.isActive = false
let search = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = search
self.navigationItem.searchController = nil
Triceps answered 3/10, 2017 at 14:28 Comment(0)
O
2

And here for Objective C

[self.navigationItem.searchController setActive:NO];
UISearchController *nilSearch = [[UISearchController alloc] initWithSearchResultsController:nil];
self.navigationItem.searchController = nilSearch;
self.navigationItem.searchController = nil;
Octo answered 2/11, 2017 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.