21.7 C
Canberra
Tuesday, October 21, 2025

searchController.searchBar fades when popping again after push to different viewcontroller


I’m attempting to show a search bar straight contained in the navigation bar (not beneath it), so it stays seen on a regular basis — just like Telegram’s design.
After I assign the search bar to the navigation bar’s titleView, it really works advantageous at first.
Nonetheless, once I push one other view controller after which navigate again, the search bar’s background fades out (turns into clear) and doesn’t restore correctly.

Right here’s the minimal reproducer:

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
    
    non-public let tableView = UITableView()
    non-public var searchController: UISearchController!
    
    non-public let allItems = ["Apple", "Banana", "Orange", "Pineapple", "Mango", "Grapes", "Watermelon"]
    non-public var filteredItems: [String] = []
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        title = "Fruits"
        view.backgroundColor = .systemBackground
        definesPresentationContext = true
        
        // MARK: - Setup TableView
        view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        // MARK: - Setup Search Controller
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search fruits"
        
        navigationItem.titleView = searchController.searchBar
        navigationItem.hidesSearchBarWhenScrolling = false
        
        filteredItems = allItems
    }
    
    // MARK: - TableView Knowledge Supply
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return filteredItems.rely
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.textual content = filteredItems[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let fruit = filteredItems[indexPath.row]
        self.navigationController?.pushViewController(FavoritesViewController(fruit: fruit), animated: true)
    }
    
    // MARK: - UISearchResultsUpdating
    func updateSearchResults(for searchController: UISearchController) {
        guard let textual content = searchController.searchBar.textual content, !textual content.isEmpty else {
            filteredItems = allItems
            tableView.reloadData()
            return
        }
        filteredItems = allItems.filter { $0.lowercased().accommodates(textual content.lowercased()) }
        tableView.reloadData()
    }
}

// MARK: - One other Instance Display screen
class FavoritesViewController: UIViewController {
    
    var fruit: String = "Favorites"
    
    init(fruit: String) {
        self.fruit = fruit
        tremendous.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        title = fruit
        view.backgroundColor = .systemBlue
    }
}

// MARK: - Root TabBarController
class MainTabBarController: UITabBarController {
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Create tabs
        let searchVC = SearchViewController()
        let favoritesVC = FavoritesViewController(fruit: "Favooor")
        
        // Embed in navigation controllers
        let nav1 = UINavigationController(rootViewController: searchVC)
        let nav2 = UINavigationController(rootViewController: favoritesVC)
        
        // Tab bar gadgets
        nav1.tabBarItem = UITabBarItem(title: "Search", picture: UIImage(systemName: "magnifyingglass"), tag: 0)
        nav2.tabBarItem = UITabBarItem(title: "Favorites", picture: UIImage(systemName: "star"), tag: 1)
        
        viewControllers = [nav1, nav2]
    }
}

The difficulty:
When navigating again from FavoritesViewController → SearchViewController,
the searchBar’s background fades and appears like this.

searchController.searchBar fades when popping again after push to different viewcontroller
search bar has the same background as its surroundings

Within the first picture, the search bar is white and distinct from its surrounding background whereas this differentiation is lacking within the second picture.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles