Its possible you're confused. You don't assign this code to a button. That code creates a button of type UIBarButtonSystemItemFixedSpace
. So, do what the answer you linked to says. Create the fixed & flexible UIBarButtonItem
s (along with the other buttons you have), then set them on your navigation bar. In this case they would appear in the top left area of your navigation bar (via leftBarButtonItems
):
// Create "Normal" buttons items:
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"1" style:UIBarButtonItemStylePlain target:Nil action:nil];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"2" style:UIBarButtonItemStylePlain target:Nil action:nil];
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithTitle:@"3" style:UIBarButtonItemStylePlain target:Nil action:nil];
// Create "Spacer" bar button items
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20.0f; // or whatever you want
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationItem.leftBarButtonItems = @[button1, fixedItem, button2, flexibleItem, button3];
Additionally, if you had a toolbar you could use the toolbar's items
property:
self.toolbar.items = @[button1, fixedItem, button2, flexibleItem, button3];