2012-08-30 52 views
1

我環顧四周,似乎無法在我的工具欄中使用自定義圖像。我在我的ViewController.m的自定義初始化部分下面的代碼,這是正確的地方添加這個?我在上方工具欄中看不到任何圖像。我想將它放在視圖的右上方。將自定義圖像添加到iPad應用程序的工具欄中

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    UIImage *image = [UIImage imageNamed:@"camera_30_30.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:image forState:UIControlStateNormal]; 

    button.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
    button.showsTouchWhenHighlighted = YES; 

    [button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

    NSMutableArray *items = [[NSMutableArray alloc] init]; 
    [items addObject:barButtonItem]; 
    self.toolbarItems = items; 
} 
return self; 
} 

感謝您的期待。

回答

2

這可能與您分配toolbarItems時相關。我認爲這將需要在UI加載後在viewDidLoad方法中設置。

你很可能也與此簡化欄按鈕創建:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithImage:@"camera_30_30.png" style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)]; 

而且,在你的代碼,你需要在年底

+0

沒有必要,如果你正在使用ARC發佈發佈項目 – jsd

+0

@ K1w1Geek,謝謝你的幫助,但我在同一條船上....我無法將這個圖標按鈕添加到我的應用程序的右上角。你能提供一些更多的幫助嗎?我嘗試了你的建議,但沒有結果。 – brianhevans

+0

右上角意味着您要添加到導航欄,而不是工具欄。這是你如何做到這一點,在UIViewController viewDidLoad方法 self.navigationItem.rightBarButtonItem = barButtonItem; – K1w1Geek

相關問題