2013-04-16 57 views
0

我剛剛有一個小問題。我發現這個代碼:一個額外的BarButtonItem

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

,但它不工作的任何想法?

編輯
我更新的代碼:如果你想有一個工具欄按鈕爲「右對齊」工具欄上,你需要

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

您不在工具欄中添加rightButton。 – random

+0

我對按鈕的瞭解非常有限,在某些情況下會令我困惑。我如何添加它? – CTSchmidt

+0

http://brianflove.com/2012/10/01/uitoolbar-for-simple-and-easy-toolbars-in-ios/ – random

回答

2

:添加按鈕的NSArray

0

試試這個在數組中添加一個靈活的空間項,在左邊的項和右邊的項之間。

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

這不是'toolbarItem'。我有一個「工具欄」,而不是「導航欄」 – CTSchmidt