如何删除左UIBarButtonItem背景

时间:2013-01-20 22:39:17

标签: ios uibarbuttonitem uibarbuttonitemstyle

我试图删除左UIBarButtonItem背景的背景。

UIBarButtonItem

我的图片只是带线条的图标。但是有黑色背景。 我的代码是:

paneViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuicon"] style:UIBarButtonItemStylePlain target:self action:@selector(navigationPaneBarButtonItemTapped:)];
paneViewController.navigationItem.leftBarButtonItem.tintColor = [UIColor clearColor];

我哪里错了?

更新

UIImage *myImage = [UIImage imageNamed:@"menuicon"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(navigationPaneBarButtonItemTapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
paneViewController.navigationItem.leftBarButtonItem = leftBarButton;

这对我有用。感谢

2 个答案:

答案 0 :(得分:5)

将对象创建为UIButton(执行所有常规设置;帧,目标,背景图像等),然后在initWithCustomView:上调用UIBarButtonItem

答案 1 :(得分:0)

这也有效:

UIBarButtonItem *navButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navbutton.png"]
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(buttonPressed)] autorelease];


NSArray *buttonArray = [NSArray arrayWithObjects:navButton, nil];

TransparentToolbar *toolbar = [[[TransparentToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0, 44.0f, 44.0f)] autorelease];
toolbar.items = buttonArray;

UIBarButtonItem *controls = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
self.navigationItem.rightBarButtonItem = controls;