0

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

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

4

2 回答 2

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];
于 2013-04-16T16:49:25.027 回答
0

尝试这个:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarStyleDefault target:self action:@selector(doSomething:)];
于 2013-04-16T16:16:48.677 回答