When I segue another page, the top of the navigation bar turns black SWIFT 5

Viewed 131

When I click on the search icon, I segue another page and added the search bar to the navigation bar.But after making a segue, the top of the navigation bar turns black.

Before segue:

screenshot before segue

After segue:

screenshot after segue

class SearchViewController: UIViewController, UISearchBarDelegate {

    let searchController = UISearchController(searchResultsController: nil)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupSearch()                    
    }
        
    func setupSearch() {
        searchController.searchBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
        navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
        navigationItem.searchController = searchController
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        searchController.searchBar.delegate = self
    }
}
3 Answers

Try adding edge for extended layout either from code like below

 self.navigationController?.navigationBar.isTranslucent = true
 self.edgesForExtendedLayout = UIRectEdge.all

Or You can do it from the XIB/Storyboard as well.

enter image description here

storyboard -> navigation controller - > untick the shows navigation bar and run again.

I solved the problem by adding backgroundcolor to my view.

view.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)

Then I added another white view. I don't know if this is the right way, but the problem is solved.

storyboard side menu screenshot

UI screenshot

Related