I'm trying to add the MDC App bar to my app as defined by MaterialComponents' custom subclass of UINavigationController
. I go by the terse description on their Github page:
The easiest integration path for using the app bar is through the MDCAppBarNavigationController. This API is a subclass of UINavigationController that automatically adds an MDCAppBarViewController instance to each view controller that is pushed onto it, unless an app bar or flexible header already exists.
When using the MDCAppBarNavigationController you will, at a minimum, need to configure the added app bar's background color using the delegate.
navigationController.pushViewController(<#T##viewController: UIViewController##UIViewController#>, animated: <#T##Bool#>) // MARK: MDCAppBarNavigationControllerDelegate func appBarNavigationController(_ navigationController: MDCAppBarNavigationController, willAdd appBarViewController: MDCAppBarViewController, asChildOf viewController: UIViewController) { appBarViewController.headerView.backgroundColor = <#(UIColor)#> }
In my storyboard, I've added a UINavigationController
and I've set its identity to MDCAppBarNavigationController
in the identity inspector.
In my root view controller, I implement MDCAppBarNavigationControllerDelegate
:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "My app"
if let nav = navigationController as? MDCAppBarNavigationController {
print("[MyViewController] Setting delegate MDCAppBarNavigationControllerDelegate")
nav.delegate = self
}
}
// MARK: MDCAppBarNavigationControllerDelegate
func appBarNavigationController(_ navigationController: MDCAppBarNavigationController,
willAdd appBarViewController: MDCAppBarViewController,
asChildOf viewController: UIViewController) {
print("Setting app bar color")
appBarViewController.headerView.backgroundColor = .red
}
}
When I run the app, I see an ordinary navigation bar and the delegate method is never called. What am I doing wrong?