The Hit List Diary #14 – Insetting Navigation Bar Items

We’ve been experimenting lately with a customised navigation bar appearance.

The bar is comprised of fairly standard items: “Cancel”, title, and “Save”. The trouble is with our customised appearance, the left and right item — Cancel and Save — ideally ought to be shuffled inwards a little more. How to achieve this?

UIBarButtonItem has this API available:

- (void)setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics

Ideally I’d like the offset to be applied automatically, to whatever items get placed in the bar. So I tried using the appearance proxy:

For the left hand button — Cancel — this works great; the title is dutifully shifted over 5 points to the right. But for the right hand item — Save — disaster! It’s shifted to the right too.

So I seem to be stuck with one of two options:

  • Apply the correct offset to each individual bar button item. This is what I’m doing presently, but it grows to be quite a pain, the more view controllers you have to manage.
  • Define separate classes — MyLeftNavBarItem, MyRightNavBarItem sort of thing — and use each’s appearance proxy accordingly

Having gotten this far in writing the post though, I’m coming to realise there’s probably a better solution (see, I knew there was a reason I write these things!).

In most navigation stacks, there are far more right hand items than left. Typically the left spot is occupied by a back button instead, in all but a handful of cases. So maybe instead my logic should be:

  • Shift all items leftwards, under the assumption they’re appearing in the right hand portion of the bar
  • Shift back buttons rightwards, if needed
  • Special case any actual left hand items to be in the correct place

Does that seem reasonable? Do you know an even better way to achieve the desired effect? Get in touch on Twitter :-)

© Mike Abdullah 2007-2015