1

我想使用会有一些图像的自定义 barbutton。我正在按照以下代码进行操作。但是 barbutton 没有显示。

 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(nowPlayingButtonClicked) forControlEvents:UIControlEventTouchUpInside];

nowPlaying = [[UIBarButtonItem alloc] initWithCustomView:button];
nowPlaying.action = @selector(nowPlayingButtonClicked);
nowPlaying.target = self;

self.navigationItem.rightBarButtonItem = nowPlaying;
4

2 回答 2

2

您必须为添加到 BarButton 的 Button 设置一个框架

   button.frame = CGRectMake(0, 0, 50, 30);
于 2012-04-20T15:21:06.203 回答
1

您必须为选定状态和正常状态提供图像。那么它就会出现在你面前。这只是一个提示。但是试试看。

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"icon_pressed.png"] forState:UIControlStateSelected];

[button addTarget:self action:@selector(nowPlayingButtonClicked) forControlEvents:UIControlEventTouchUpInside];

nowPlaying = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = nowPlaying;
于 2012-04-20T15:14:02.823 回答