Search code examples
iosobjective-cuibuttonuibarbuttonitemuitoolbar

a additional BarButtonItem


I just got a little problem. I found this code:

MKUserTrackingBarButtonItem *trackingButton = [[MKUserTrackingBarButtonItem alloc]initWithMapView:self.mapView];
[self.mapToolbar setItems: [NSArray arrayWithObject:trackingButton] animated:YES];

and I want to add an additional button on the right side (at left is the tracking button).
NOTE
its is a UIToolbar not a navigationbar

so i just tried this:

   MKUserTrackingBarButtonItem *trackingButton = [[MKUserTrackingBarButtonItem alloc]initWithMapView:self.mapView];
    UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Pins" style:UIBarButtonItemStyleBordered target:self action:@selector(rightButton:)];
    [self.mapToolbar setItems: [NSArray arrayWithObjects:trackingButton, rightButton, nil] animated:YES];

but it isn't working any ideas?

EDIT
I updated the code: added button to NSArray


Solution

  • If you want a tool bar button to be "right justified" on toolbar you need to add a flexible space item in the array in between the item on the left and the item on the right.

    MKUserTrackingBarButtonItem *trackingButton = [[MKUserTrackingBarButtonItem alloc]initWithMapView:self.mapView];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Pins" style:UIBarButtonItemStyleBordered target:self action:@selector(rightButton:)];
    [self.mapToolbar setItems: [NSArray arrayWithObjects:trackingButton, flexible, rightButton, nil] animated:YES];