I've spent several hours on trying to figure this out to no end. My last option is to just create blank labels to create space, but I feel like their is a more cleaner way.
Basically I have three buttons and we're trying to create fixed space between them for neatness. Each button is programmatically added.
I found this code:
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20.0f; // or whatever you want
(Source)
But how do you assign this piece of code to a certain button?
For adjusting space between your navbar elements you need to give padding. You can also use margin, but I don't recommend this. Add the padding as much as you can. Also, use Chrome Developers Tool(Inspect element) to see the perfect padding for your element.
If you want some space at the beginning and end, simply change justify-content: space-between; to justify-content: space-around; .
Simply add px-3 (or a number of your choice) inside the class="" attribute of your chosen nav-item.
Add the border property to <ul> add a border around the navbar. If you also want borders inside the navbar, add a border-bottom to all <li> elements, except for the last one: Home.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With