2

我试图删除左侧 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;

这对我有用。谢谢

4

2 回答 2

5

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

于 2013-01-20T22:59:06.670 回答
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;
于 2013-08-08T21:31:04.837 回答