Aligning Uitoolbar Items

Aligning UIToolBar items

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.

How to align UIBarButtonItem in UIToolbar in iOS universal app?

You can do it all in your Storyboard.

Select a Bar Button Item in the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Item at its right. Add your second Bar Button Item at its right. Then, add a second Flexible Space Bar Button Item at its right.

The result will look like this in Interface Builder:

Sample Image

If necessary, you can add an extra Fixed Space Bar Button Item at the right edge of your Toolbar to be sure that your second Bar Button Item is really centered.

User lxt gives another example of this in answer for a similar question here.

Edit: Be sure that your UIToolbar has good constraints before proceeding!

How to align 2 buttons to left & right border of a UIToolbar

Yes, just add a flexible space in between the two of them.

let toolbar = UIToolbar(frame: CGRectMake(0, 0, w, 35))
toolbar.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 0.95)
let acceptButton = UIBarButtonItem(title: "Accept", style: UIBarButtonItemStyle.Done, target: self, action: "buttonPressed:")
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonPressed:")
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil);
toolbar.items = [acceptButton, flexibleSpace, cancelButton]

Not sure if that is the correct syntax, but the class is still UIBarButtonItem. The only difference is the systemItem is a UIBarButtonSytemItem.FlexibleSpace.

Comments:

So in another SO answer this was the suggestion:

UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item

This doesn't work either, seems like no direct subview-child is a
UIBarButtonItem (and so it returns the width as 375).

The reason that doesn't work is because [self.toolbar.subviews objectAtIndex:0] is Objective-C code, not Swift code.

How do I align Ti.UI.Toolbar items? (Android Appcelerator)

Currently on Android I would propose this workaround - place the views in a single View that is passed as an item for the toolbar. Try the following:

index.xml

<Alloy>
<Window>
<!-- Add id for the toolbar to be accessed from index.js-->
<Toolbar width="Ti.UI.FILL" bottom="0" barColor="#639851" id="bar">
<Items>
<!-- Add the view that acts as a container-->
<View class="wrapper">
<!-- Set a relative offset on the left since system buttons are not with exactly one third width-->
<View left="8%" id="addView" class="bottomBtn">
<ImageView class="icons" image="/images/git.png" touchEnabled="false" />
<Label class="label" text="Add" touchEnabled="false" />
</View>

<View id="mapView" class="bottomBtn">
<ImageView class="icons" image="/images/git.png" touchEnabled="false" />
<Label class="label" text="See map" touchEnabled="false" />
</View>
<!-- Set a relative offset on the right since system buttons are not with exactly one third width-->
<View right="8%" id="routeView" class="bottomBtn">
<ImageView class="icons" image="/images/git.png" touchEnabled="false" />
<Label class="label" text="Calculate" touchEnabled="false" />
</View>
</View>
</Items>
</Toolbar>
</Window>
</Alloy>

Add the following line in index.js

$.bar.setContentInsetsAbsolute(0,0);

It will extend the container for custom views of the Toolbar to the full width of the component.

index.tss

".label" : {
color: "#212121"
}

".icons" : {
width: "24dp",
height: "24dp"
}

".bottomBtn" : {
layout: 'vertical',
width: '28%',
height: Ti.UI.SIZE,
backgroundColor: "#639851",
touchFeedback: true,
touchFeedbackColor: "#808080"
}

".wrapper" : {
width: Ti.UI.FILL,
height: Ti.UI.SIZE,
layout: 'horizontal',
horizontalWrap: false
}

You can play around with the percentage values to get a different alignment based on the system navigation buttons.

Edit: And of course add the paths to your resources.

Can I center a UIToolbar item?

Simplest way would be to put the label above the bar, in it's parent view, and justify it there.

Horizontally centering all items in UIToolbar, including custom view with UILabel

One solution would be to put your UILabel *label inside a 'container' UIView with a y origin of the *label label at maybe -2px. Then set the 'container' UIView as the view for the UIBarButtonItem

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:17];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.text = @"The Title";
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];

UIView *labelContainerView = [[UIView alloc]initWithFrame:label.frame];

//adjust the origin
CGRect labelFrame = label.frame;
labelFrame.origin.y = -2;
label.frame = labelFrame;

[labelContainerView addSubView:label];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:labelContainerView];


Related Topics



Leave a reply



Submit