UIButton 在 iOS 中不显示

发布于 2024-11-29 18:09:56 字数 411 浏览 3 评论 0原文

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

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

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

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

Following is a code to display a button to the toolbar.

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

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

Nothing gets displayed on the toolbar. Please tell me what is wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-12-06 18:09:56
[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];
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help

OR if it's a navigation bar

self.navigationItem.rightBarButtonItem = myToolbarButton;

Also you should set the frame for the custom button, something like this:

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

Then if you want to add a selector/callback:

[myButton addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
禾厶谷欠 2024-12-06 18:09:56
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.

其中 self 是 UIViewController。

self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.

Where self is a UIViewController.

时光匆匆的小流年 2024-12-06 18:09:56

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

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)];

You forgot to add this button to the toolbar!

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];

If you want to add a button to navigation bar, use this:

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

Also, you should remember to set the frame of myButton:

[myButton setFrame:CGRectMake(0, 0, 30, 32)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文