Aligning UIToolBar items
Asked Answered
F

4

117

I have three UIBarButtonItem created as below. They align left and I'd like to align center so there isn't a gap on the right side. I don't see an align property on UIToolBar. Is there another way to accomplish this?

//create some buttons
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAbout:)];
[toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
Frenzy answered 2/3, 2009 at 15:21 Comment(0)
N
263

Add two UIBarButtonSystemItemFlexibleSpace items to your toolbar, to the left and right of your items

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];

Adding these as you would any other toolbar items will distribute space evenly between the two of them.

Nakashima answered 2/3, 2009 at 15:42 Comment(4)
There's no real reason to make multiple flexible spaces. You can add the same multiple flexible space object to your toolbar multiple times if you need more than one.Languet
mmc is correct. In fact, it would probably be perfectly reasonable to make a singleton flexibleSpace and reuse it throughout your product. I have heard it claimed that this was the true original use of singletons: not to enforce program rules, but to reduce overhead.Warrantable
You forgot to update the release call, should read [flexibleSpace release];Aaronaaronic
Yeah, sorry, it read [flexibleSpaceLeft release] instead of [flexibleSpace release]. Typo fixedNakashima
B
32

This can also be done right from a storyboard.

Just drag and drop items in the toolbar, and turn some of them into flexible or fixed space to get the desired effect. See the two examples below.

Evenly spaced

Centered

Brainsick answered 28/11, 2014 at 14:55 Comment(0)
Y
9

In Xamarin iOS

Right aligned:

yourBar.SetItems(new [] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), yourButton }, false);

Center Aligned:

var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
yourBar.SetItems(new [] { flexibleSpace, yourButton, flexibleSpace}, false);
Yoshida answered 24/1, 2013 at 7:10 Comment(6)
This is not the topic. We are talking about pure iOS here.Frodine
Which is what MonoTouch targets.Yoshida
Objective C question not MonoTouch/C#Ingressive
@MaxMacLeod: it's not tagged obj-c, it's tagged iPhone. Therefore my answer is valid. It's also good to contrast c# vs obj-c. Clearly, c# is modern, slimmer, nicer, faster and superior in every way.Yoshida
While it may have been clear that the code in the question was Objective-C, I found this answer helpful. It's a high ranking question in Google too.Kippie
Not a good answer for the primary question, but was helpful for me in working with Xamarin. Thanks!Unpolitic
G
9

Swift version:

    let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: viewController.view.frame.size.width, height: 35.0))
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: viewController, action: nil)
    let button1 = UIBarButtonItem(title: "A", style: UIBarButtonItem.Style.Plain, target: viewController, action: foo)
    let button2 = UIBarButtonItem(title: "B", style: UIBarButtonItem.Style.Plain, target: viewController, action: bar)
    let button3 = UIBarButtonItem(title: "C", style: UIBarButtonItem.Style.Plain, target: viewController, action: blah)
    toolbar.items = [button1, flexibleSpace, button2, flexibleSpace, button3]
Garneau answered 6/5, 2016 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.