一个额外的BarButtonItem

时间:2013-04-16 15:13:19

标签: ios objective-c uibutton uibarbuttonitem uitoolbar

我遇到了一个小问题。我找到了这段代码:

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

我想在右侧添加一个额外的按钮(左边是跟踪按钮)。
注意
它是一个UIToolbar而不是导航栏

所以我试过这个:

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

但它没有任何想法?

编辑
我更新了代码:添加按钮到NSArray

2 个答案:

答案 0 :(得分:2)

如果要在工具栏上“右对齐”工具栏按钮,则需要在数组中在左侧项目和右侧项目之间添加灵活空间项目。

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

答案 1 :(得分:0)

试试这个:

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