iOS 14.5 - [Assert] Someone is removing an active search controller while its search bar is visible

515 views Asked by At

Started getting the below mentioned error, with the upgrader to iOS Version 14.5. It works fine in 14.4 version without any issues. This is related to the search bar. When the text is typed, it doesn't open the keyboard and freeze the screen. It should shows the below error message which is not clear enough to be fixed.

"[Assert] Someone is removing an active search controller while its search bar is visible. The UI probably looks terrible. Search controller being removed: <UISearchController: 0x7f8d75c86000> from navigation item:"

It is written in SWIFT UI

Code uses

struct SearchBarView: UIViewControllerRepresentable to create the searchBar

Using lates version of Xcode 12.5 and Simulator iOS 14.5

Is there a fix for it? Suggestions welcomed to fix the issue.

Code Snippet:

import Foundation import SwiftUI import Combine

struct SearchBarView: UIViewControllerRepresentable {

@Binding var text: String
@Binding var scopeTitles: [String]
@Binding var selectedScopeIndex: Int

init(text: Binding<String>, scopeTitles: Binding<[String]>, selectedScopeIndex: Binding<Int>) {
    self._text = text
    self._scopeTitles = scopeTitles
    self._selectedScopeIndex = selectedScopeIndex
}

func makeUIViewController(context: Context) -> SearchBarWrapperController {
    return SearchBarWrapperController()
}

func updateUIViewController(_ controller: SearchBarWrapperController, context: Context) {
    controller.searchController = context.coordinator.searchController
}

func makeCoordinator() -> Coordinator {
    return Coordinator(text: $text, scopeTitles: $scopeTitles, selectedScopeIndex: $selectedScopeIndex)
}

class Coordinator: NSObject, UISearchResultsUpdating {
    
    @Binding var text: String
    @Binding var scopeTitles: [String]
    @Binding var selectedScopeIndex: Int
    
    let searchController: UISearchController
    private var subscription: AnyCancellable?
    
    init(text: Binding<String>, scopeTitles: Binding<[String]>, selectedScopeIndex: Binding<Int>) {
        self._text = text
        self._scopeTitles = scopeTitles
        self._selectedScopeIndex = selectedScopeIndex
        self.searchController = UISearchController(searchResultsController: nil)
        
        super.init()
        
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.automaticallyShowsScopeBar = false
        searchController.searchBar.scopeButtonTitles = scopeTitles.wrappedValue
        searchController.searchBar.showsScopeBar = true
        
        self.searchController.searchBar.text = self.text
        self.subscription = self.text.publisher.sink { _ in
            self.searchController.searchBar.text = self.text
        }
    }
    
    deinit {
        self.subscription?.cancel()
    }
    
    func updateSearchResults(for searchController: UISearchController) {
        guard let text = searchController.searchBar.text else { return }
        DispatchQueue.main.async {
            self.text = text
            self.selectedScopeIndex = searchController.searchBar.selectedScopeButtonIndex
        }
    }
}

class SearchBarWrapperController: UIViewController {
    var searchController: UISearchController? {
        didSet {
            self.setupView()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.setupView()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.setupView()
    }
    
    func setupView() {
        self.parent?.navigationItem.searchController = searchController
        self.parent?.navigationItem.hidesSearchBarWhenScrolling = false
        self.parent?.navigationController?.navigationBar.sizeToFit()
    }
}

}

0

There are 0 answers