I test in an App as follow, and:
1.The Big Title change to small title when scrolling
2. The Search Bar NOT stay permanently under the Navigation bar (In RootViewController), it stay permanently in SecondViewController (because it’s view property is a normal UIView).
3. Some pages are able to change to small tilte when scrolling (You control it by choosing between the different options of navigationItem.largeTitleDisplayMode)
(Is Initial ViewController) UINavigationController ————> class RootViewController: UITableViewController ————> class SecondViewController: UIViewController
In Interface Builder:
- In RootViewController:
- Set the Identifier of UITableViewCell to CellIdentifier.
- Add an UIBarButtonItem and from it, add a Push Segue to SecondViewController.
import UIKit
extension RootViewController: UISearchControllerDelegate{
func willPresentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func didPresentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func willDismissSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func didDismissSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func presentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
}
extension RootViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
}
class RootViewController: UITableViewController {
let array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"]
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
// Adopt Large Titles:
// Set up the navigation bar
navigationItem.title = "Custom Title"
navigationController?.navigationBar.prefersLargeTitles = true
searchController.delegate = self
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.textLabel?.text = array[indexPath.item]
return cell
}
}
- In SecondViewController choose between the different options of navigationItem.largeTitleDisplayMode:
import UIKit
extension SecondViewController: UISearchControllerDelegate{
func willPresentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func didPresentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func willDismissSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func didDismissSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
func presentSearchController(_ searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
}
extension SecondViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController){
print("\(type(of: self)) \(#function)")
}
}
class SecondViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
// Automatically Choose Large Title
navigationItem.largeTitleDisplayMode = .automatic //.automatic --> Keep the UINavigationBar in whichever state it was for the previous screen.
//navigationItem.largeTitleDisplayMode = .always //.always --> Show the UINavigationBar with large titles regardless of which state it was in for the previous screen.
// navigationItem.largeTitleDisplayMode = .never //.never --> Never display a large title.
searchController.delegate = self
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
}
}