iosios-autolayout

UIViewController wrong size after UISearchController


I have an UIViewController and in this I show UISearchController in the navigationbar. In ios11 . + this will increase the navigationbar height. That's okay for me. So the user click on my button to show the searchbar, and I show it. If the user click on the cancel button I remove the searchbar. The problem is occuring when the user click on the button to show the searchbar and after this, the user wants to go another UIViewController. I load the another UIViewController and I got a black bar between the navigationbar and the content. I logged out the height of the view:

These are the ideal sizes:

viewDidLoad - NavBar SIZE: 44.000000

viewDidLoad - view SIZE: 667.000000

viewWillAppear - NavBar SIZE: 44.000000

viewWillAppear - view SIZE: 603.000000

And these are the wrong sizes:

viewDidLoad - NavBar SIZE: 44.000000

viewDidLoad - view SIZE: 667.000000

viewWillAppear - NavBar SIZE: 44.000000

viewWillAppear - view SIZE: 551.000000

You can see that the height of the view in the viewWillAppear method is smaller than expected. Why? I tried to force update the layout:

[self.view layoutIfNeeded];
[self.view setNeedsLayout];

But its not working.


Solution

  • ios11 + Navigation bar + Searchbar always creates problems since it launched

    I have faced similar issue when View Size is different when I push View controller from View controller which has search bar. and shows BLACK BAR next to the Navigation bar

    There are number of solution available like fixed height of Search bar but it behaves unexpectedly

    For that I found solution to create subclass of UIView

    class SearchBarContainerView: UIView {
    
        let searchBar: UISearchBar
    
        init(customSearchBar: UISearchBar) {
            searchBar = customSearchBar
            super.init(frame: CGRect.zero)
    
            addSubview(searchBar)
        }
    
        override convenience init(frame: CGRect) {
            self.init(customSearchBar: UISearchBar())
            self.frame = frame
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            searchBar.frame = bounds
        }
    }
    

    And then use this as Title View

        // Search bar
        searchController = UISearchController(searchResultsController: nil)
        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.searchBar.backgroundColor = .clear
        self.searchController.searchBar.placeholder = nil
    
        self.definesPresentationContext = true
    
        let searchBarContainer = SearchBarContainerView(customSearchBar: self.searchController.searchBar)
    
        searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)
        navigationItem.titleView = searchBarContainer
    

    Hope it is helpful to you in your case