2012-01-13 99 views
0

我有以下代码:将按钮添加到工具栏底部

self.shareButton=[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 35, 35)]; 
[shareButton setBackgroundColor:[UIColor blueColor]]; 
[shareButton setBackgroundImage:[UIImage imageNamed:@"share_button"] forState:UIControlStateNormal]; 
[shareButton setBackgroundImage:[UIImage imageNamed:@"share_button_pressed"] forState:UIControlStateHighlighted]; 
[shareButton addTarget:self action:@selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.navigationController setToolbarItems:[NSArray arrayWithObject:shareButton] animated:YES]; 

为什么我不能看到工具栏按钮?有什么问题?

+2

尝试使用UIBarButtonItem而不是UIButton? – meggar 2012-01-13 16:37:50

+0

新增了'UIBarButtonItem * shareBarButton = [[UIBarButtonItem alloc] initWithCustomView:shareButton];'并更改了'[self.navigationController setToolbarItems:[NSArray arrayWithObject:shareBarButton] animated:YES];'问题依然存在 – Oleg 2012-01-13 16:42:45

回答

1

meggar说的可能是正确的答案。试试这样做。

--UPDATE - 由于问题仍然存在,我更新了示例将set调用移动到视图控制器而不是导航控制器。

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton]; 
[self setToolbarItems:[NSArray arrayWithObject:barButtonItem] animated:YES]; 
[barButtonItem release]; 
+0

问题依然存在:( – Oleg 2012-01-13 16:45:27

+1

我稍微更新了这个例子。 – jaminguy 2012-01-13 16:48:00

+0

是的!这解决了我的问题,非常感谢 – Oleg 2012-01-13 16:50:37

相关问题