How to Add Multiple UIBarButtonItems to UINavigationBar

Posted:

Here's how to do it for iOS 5+.

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create the refresh, fixed-space (optional), and profile buttons.
    UIBarButtonItem *refreshBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];

    //    // Optional: if you want to add space between the refresh & profile buttons
    //    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //    fixedSpaceBarButtonItem.width = 12;

    UIBarButtonItem *profileBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Profile" style:UIBarButtonItemStylePlain target:self action:@selector(goToProfile)];
    profileBarButtonItem.style = UIBarButtonItemStyleBordered;

    self.navigationItem.rightBarButtonItems = @[profileBarButtonItem, /* fixedSpaceBarButtonItem, */ refreshBarButtonItem];
}

The above only works for iOS 5+.

I recommend only supporting iOS 5+ because making your app better for iOS 5+ is more valuable than supporting iOS < 5. Trust me.

But, if you must support iOS < 5, replace the last line of -viewDidLoad above with the code below.

    // Add the buttons to the right of the navigation bar.
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0 // iOS Deployment Target
    self.navigationItem.rightBarButtonItems = @[profileBarButtonItem, /* fixedSpaceBarButtonItem, */ refreshBarButtonItem];
#else
    if ([self.navigationItem respondsToSelector:@selector(rightBarButtonItems)]) {
        self.navigationItem.rightBarButtonItems = @[profileBarButtonItem, /* fixedSpaceBarButtonItem, */ refreshBarButtonItem];
    } else {
        // Support iOS < 5.0: by first adding the buttons to a toolbar.
        // Note: You could also do this by creating two UIButtons, both with the same custom background image as a UIBarButtonItem.
        //       Give the refresh button the refresh arrow as its image and the profile button the title @"Profile".
        //       You can get Apple's images for the UIBarButtonItem background and the refresh arrow by monkey patching -[UIImage imageNamed:] and/or other similar methods to save each image to a file on disk.
        //       Then, run your app in the simulator or on your device, and the files will automatically appear where you programmed to save them.
        //       You may also find these images in Apple's resources, e.g., by running the following command in Terminal `find /Applications/Xcode.app/ -name *.png`
        //       Otherwise, you can make these images yourself.
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)]; // 44.01 shifts it up 1px for some reason
        toolBar.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
        // anyone know how to get it perfect?
        toolBar.barStyle = -1; // clear background
        [toolBar setItems:@[refreshBarButtonItem, /* fixedSpaceBarButtonItem, */ profileBarButtonItem] animated:NO];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
    }
#endif

See how much more work that was? Definitely not worth it since now less than 0.5% of users installing your app run iOS < 5.

Moral of the story: Only support iOS 5+, not iOS 4.3, and you probably wouldn't have even had to look this up. :-)