actually
you can’t… or at least I can’t find a way to get UIBarMetrics
either from self.navigationBar
or from self.navigationItem
. There is simply no public getter methods in all UINavigationBar
related API’s!
What can we do?
By relying on UITraitCollection
and getting horizontal and vertical size classes we can deduce if we are in a regular or a compact condition. App’s navigation bar enters in compact metrics when both horizontal and vertical size classes are compact.
0 1 2 3 4 5 6 7 8 9 |
UITraitCollection *triat = self.traitCollection; BOOL compact = (triat.horizontalSizeClass == UIUserInterfaceSizeClassCompact) && (triat.verticalSizeClass == UIUserInterfaceSizeClassCompact); UIBarMetrics metrics; if (compact) metrics = UIBarMetricsCompact; else metrics = UIBarMetricsDefault; |
This way we cover UIBarMetricsDefault
and UIBarMetricsCompact
, leaving to the further deduction UIBarMetricsDefaultPrompt
and UIBarMetricsCompactPrompt
. But at least it’s something!
If you also need to adapt dynamically when the iOS interface environment changes (e.g. when the device is rotated), then you need to listen to UIViewController
conforming UITraitEnvironment
protocol delegate callback traitCollectionDidChange:
0 1 2 3 4 5 6 |
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange: previousTraitCollection]; //leveraging on the code above adapt the interface to the size change } |