I have a tableview displaying on a main view controller, and when a row is selected a detail view controller is pushed. I have a large title for the main view controller, and a small/regular title for the detail view controller. They are embedded in a navigation controller and tab bar controller.
Before selecting a row, the main view controller title is large, and when a row is selected the detail view controller title is regular as it should be. However when I return to the main view controller from the detail view controller (via "back" button), the title on the main view controller is no longer large.
I have "prefersLargeTitles" set to true on the main view controller, and "largeTitleDisplayMode" set to never on the detail view controller.
I have tried setting "largeTitleDisplayMode" to always on the main view controller to no avail. I've also tried setting it to automatic on either view controller and seems to have no effect.
I've also tried using "viewWillAppear" and "viewWillDisappear" and setting the title in there, and while it does indeed reset the main view controller title back to large, the animation lags and isn't smooth like it normally is when transitioning from a small title to a large title.
Also I'm pretty new to coding and this is my first app I'm building without using storyboards, so the code could be a mess.
MAIN view controller code:
class HomeViewController: UIViewController {
let tableView = UITableView()
override func loadView() {
super.loadView()
view.backgroundColor = .white
self.title = "Home"
// Set large title
navigationController?.navigationBar.prefersLargeTitles = true
// Make navigation bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
extension HomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = DetailViewController() as DetailViewController
if indexPath.section == 0 {
vc.detailTitle = itemsTop[indexPath.row]
} else if indexPath.section == 1 {
vc.detailTitle = itemsBottom[indexPath.row]
} else {
print("Failed to load title")
}
navigationController?.pushViewController(vc, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
}
DETAIL view controller code:
class DetailViewController: UIViewController {
var detailTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
title = detailTitle
// Make nav bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
// Prevent large title
navigationController?.navigationBar.prefersLargeTitles = false
}
}
Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .
From the outline view, make sure your Table View Controller is selected. Then go to the Editor menu, and click on the Embed In submenu, and choose Navigation Controller and voila. You have your navigation controller pointing to your tableview controller with a relationship built in. This should be the selected answer.
try this it's working. In 'HomeViewController'
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
}
In 'DetailViewController'
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.largeTitleDisplayMode = .never
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With