How to get current UIBarMetrics value

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.

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:

 

Leave a Reply