1

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?

Maarten
  • 6,894
  • 7
  • 55
  • 90

1 Answers1

1

After different tries I made it working by creating a custom MDCAppBarNavigationController and set it to my UINavigationController in the Storyboard (Identity tab -> Class)

Here's my custom MDCAppBarNavigationController:

import MaterialComponents.MDCAppBarNavigationController

class MaterialNavigationController: MDCAppBarNavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        delegate = self
    }
}

extension MaterialNavigationController: MDCAppBarNavigationControllerDelegate {
  func appBarNavigationController(_ navigationController: MDCAppBarNavigationController, willAdd appBarViewController: MDCAppBarViewController, asChildOf viewController: UIViewController) {

    appBarViewController.applyTheme()
    appBarViewController.navigationBar.applyTheme()
  }
}
Azartys
  • 543
  • 6
  • 14