I am experiencing a problem with the new way of adding search bar to the navigation item.
As you can see in the picture below, there are two UIViewControllers one after the other, and both have the search bar. The problem is the animation, which is ugly when search bar is visible on the first view controller but not on the second one. The area occupied by the search bar stays on the screen and suddenly disappears.
The code is very basic (no other changes in the project were made):
(I write primarily in C#, so there might be errors in this code.)
ViewController.swift:
import UIKit
class ViewController: UITableViewController, UISearchResultsUpdating {
override func loadView() {
super.loadView()
definesPresentationContext = true;
navigationController?.navigationBar.prefersLargeTitles = true;
navigationItem.largeTitleDisplayMode = .automatic;
navigationItem.title = "VC"
tableView.insetsContentViewsToSafeArea = true;
tableView.dataSource = self;
refreshControl = UIRefreshControl();
refreshControl?.addTarget(self, action: #selector(ViewController.handleRefresh(_:)), for: UIControlEvents.valueChanged)
tableView.refreshControl = refreshControl;
let stvc = UITableViewController();
stvc.tableView.dataSource = self;
let sc = UISearchController(searchResultsController: stvc);
sc.searchResultsUpdater = self;
navigationItem.searchController = sc;
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell1");
if (cell == nil) {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell1");
}
cell?.textLabel?.text = "cell " + String(indexPath.row);
return cell!;
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20;
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = ViewController();
navigationController?.pushViewController(vc, animated: true);
}
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
refreshControl.endRefreshing();
})
}
func updateSearchResults(for searchController: UISearchController) {
}
}
AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds);
window?.rootViewController = UINavigationController(rootViewController: ViewController());
window?.makeKeyAndVisible();
UINavigationBar.appearance().barTintColor = UIColor.red;
return true
}
}
Ideas?
viewDidLoad()
(instead of loadView() ) β Megillah