0

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

CTSchmidt
  • 1,155
  • 3
  • 17
  • 30

2 Answers2

2

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];
Scott Ahten
  • 1,141
  • 7
  • 15
0

Try this:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarStyleDefault target:self action:@selector(doSomething:)];
Kevin
  • 483
  • 3
  • 12