To add this component to your Xcode project using CocoaPods, add the following to your Podfile
:
pod 'MaterialComponents/NavigationBar'
Then, run the following command:
pod install
Navigation Bar is a drop-in replacement for UINavigationBar with a few notable exceptions:
The MDCNavigationBar class is a composition of two Button Bars and a title label or title view. The left and right Button Bars are provided with the navigation item's corresponding bar button items.
Read the Button Bar section on UIBarButtonItem properties to learn more about supported UIBarButtonItem properties.
Before using Navigation Bar, you'll need to import it:
import MaterialComponents.MaterialNavigationBar
#import "MaterialNavigationBar.h"
MDCNavigationBar can observe changes made to a navigation item property much like how a UINavigationBar does. This feature is the recommended way to populate the navigation bar's properties because it allows your view controllers to continue using navigationItem
as expected, with a few exceptions outlined below.
If you intend to use UINavigationItem observation it is recommended that you do not directly set the navigation bar properties outlined in
MDCUINavigationItemObservables
. Instead, treat the observednavigationItem
object as the single source of truth for your navigationBar's state.
To begin observing a UINavigationItem instance you must call observeNavigationItem:
.
navigationBar.observe(navigationItem)
[navigationBar observeNavigationItem:self.navigationItem];
navigationBar.unobserveNavigationItem()
[navigationBar unobserveNavigationItem];
All of the typical properties including UIViewController‘s title
property will affect the Navigation Bar as you’d expect, with the following exceptions:
animated:
method varients are supported because they do not implement KVO events. Use of these methods will result in the Navigation Bar becoming out of sync with the navigationItem properties.prompt
is not presently supported. https://github.com/material-components/material-components-ios/issues/230.