0

以下是显示工具栏按钮的代码。

UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:@"img1.png"];

[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];

工具栏上没有显示任何内容。请告诉我有什么问题。

4

3 回答 3

2
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help

或者如果它是一个导航栏

self.navigationItem.rightBarButtonItem = myToolbarButton;

您还应该为自定义按钮设置框架,如下所示:

UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];

然后,如果您想添加选择器/回调:

[myButton addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
于 2011-08-17T10:08:29.693 回答
0
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.

其中 self 是一个 UIViewController。

于 2011-08-17T10:09:56.747 回答
0

您忘记将此按钮添加到工具栏!

NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];

如果要向导航栏添加按钮,请使用以下命令:

[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...

另外,您应该记住设置的框架myButton

[myButton setFrame:CGRectMake(0, 0, 30, 32)];
于 2011-08-17T10:10:28.717 回答