将UIBarButtonItem设置为没有背景的图像

时间:2013-07-24 21:19:52

标签: objective-c

目前我正在将我的UIBarButtonItem设置为具有透明背景的40x40像素图像。当我将UIBarButtonItem设置为此图像时,它会产生以下结果:

enter image description here

我只想要没有绿色背景的家伙的形象。我应该怎么做呢?

我试过的代码:

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"profile-barbutton.png"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(myselector)];

还尝试了以下内容(以及使用initWithTitle初始化栏:@“”......

工作但选择器不再引用UINavigationController

以下代码生成异常[MasterViewController toggleMenu]:无法识别的选择器,因为选择器位于我的NavigationController类中,该类先前已使用UIBarButtonItem引用但不再是。

      UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0f, 40.0f)];
    [profileBarButton setImage:[UIImage imageNamed:@"profile-barbutton.png"] forState:UIControlStateNormal];
    [profileBarButton addTarget:self action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];



    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];

    [self.navigationItem.leftBarButtonItem setTarget:self.navigationController];
    [self.navigationItem.leftBarButtonItem setAction:@selector(toggleMenu)];

1 个答案:

答案 0 :(得分:1)

您想要设置背景图像,而不是图像。试试这个:

[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage imageNamed:@"profile-barbutton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];