image 1 this is the initial estimated behaviour. image 2 after searchbar became active this is the behaviour. image 3 once after changing the orientation to landscape, keyboard dismissed along with that search bar gets defocused and even after tapping cancel, Cancel button is not hiding.
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
// Table view and search controller
private var tableView: UITableView!
private var searchController: UISearchController!
// Data source
private var data: [String] = ["Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon"]
private var filteredData: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
title = "My View Controller"
view.backgroundColor = .systemBackground
// Set up table view
tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Set up search controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
definesPresentationContext = true
}
// MARK: - Table View DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredData.count
} else {
return data.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
let item: String
if isFiltering() {
item = filteredData[indexPath.row]
} else {
item = data[indexPath.row]
}
cell.textLabel?.text = item
return cell
}
// MARK: - Table View Delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Handle row selection if needed
}
// MARK: - Search Controller
func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text ?? ""
filterContent(for: searchText)
}
func filterContent(for searchText: String) {
filteredData = data.filter { item in
return item.lowercased().contains(searchText.lowercased())
}
tableView.reloadData()
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
func searchBarIsEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? true
}
}
this sample implementation is absolute working fine in regular devices.