2012-08-30 50 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

相关问题