How to create fixed space and flexible space bar button items programmatically?

76,394

Solution 1

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];

Solution 2

Swift

// Fixed Space
let fixedSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
fixedSpace.width = 20.0

// Flexible Space
let flexibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

Solution 3

UIBarButtonItem *todayItem = [[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)];
todayItem.tag = 2;

UIBarButtonItem *cashItem = [[UIBarButtonItem alloc] initWithTitle:@"Cash" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)];
cashItem.tag = 3;

UIBarButtonItem *creditItem = [[UIBarButtonItem alloc] initWithTitle:@"Credit" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)];
creditItem.tag = 4;

UIBarButtonItem *allItem = [[UIBarButtonItem alloc] initWithTitle:@"All" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)];
allItem.tag = 1;

UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] initWithTitle:@"Return" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)];
returnItem.tag = 5;

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedItem setWidth:455.0f];

UIBarButtonItem *fixed2Item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixed2Item setWidth:37.0f];

UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[self.toolbar setItems:@[fixed2Item, returnItem, creditItem, cashItem, fixedItem, todayItem, flexibleItem, allItem] animated:NO];

Solution 4

As of this writing, if you're targeting iOS 14 and above, you can use the corresponding class functions to get fixed and flexible space items more concisely:

let fixedSpace = UIBarButtonItem.fixedSpace(20)
let flexibleSpace = UIBarButtonItem.flexibleSpace()

Docs: fixed space, flexible space

Solution 5

In Swift:

let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixedSpace.width = 20.0
Share:
76,394
Linux world
Author by

Linux world

enered in to linux scripting ... working on SAN based...

Updated on March 10, 2022

Comments

  • Linux world
    Linux world about 2 years

    I want to create UIBarButtonItems programmatically and place these fixed space items between buttons.

  • Nick Forge
    Nick Forge almost 13 years
    Note that to set the width of a Fixed Space UIBarButtonItem, you need to set the .width property.
  • Ben Lachman
    Ben Lachman almost 11 years
    Why are you setting right bar button items to left items? That's just really bad style or a bug and I don't know which.
  • Pratik Jamariya
    Pratik Jamariya over 7 years
    perfect answer!
  • Satyam
    Satyam about 7 years
    how can we have flexible width give equal spacing between all the buttons using code? I don't want to hard code width=20.0
  • heyfrank
    heyfrank almost 4 years
    Just put one flexible width between each two buttons @Satyam