6

I'm using the setBackButtonTitlePositionAdjustment:forBarMetrics: method on UIBarButtonItem's appearance like so:

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(12, -1), forBarMetrics: .Default)
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(15, -1), forBarMetrics: .Compact)

Then, in a view controller I'd like to use backButtonTitlePositionAdjustment(for:) to get the value. I assumed there must be a UIBarMetrics value I could use - perhaps in the navigation bar or a UITraitCollection, but I think I'm missing something obvious?

chrishale
  • 2,436
  • 2
  • 18
  • 19

1 Answers1

1

you can retrieve the value in a view controller like this

if let backButtonItem = navigationItem.backBarButtonItem {
            let offset = backButtonItem.backButtonTitlePositionAdjustment(for: .default)
            print(offset) 
        }

we check if the backBarButtonItem property of the current view controller's navigationItem is not nil. If it exists, we retrieve the back button title position adjustment using the backButtonTitlePositionAdjustment(for:) method, passing the .default bar metrics as the parameter. The method will return the offset adjustment, which you can then use as needed.

Note that if the backBarButtonItem is nil, it means that the back button is being inherited from the previous view controller or the navigation item does not have a back button set. In this case, you might need to handle it differently depending on your specific requirements

Sudeep P H
  • 244
  • 5