看不到我的导航栏自定义按钮,但它是可点击的

发布于 2024-10-21 06:58:02 字数 798 浏览 0 评论 0原文

我想在导航栏中添加一个自定义按钮(右侧)。

我的“viewDidLoad”函数中有以下代码:

UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:@"Play" forState:UIControlStateNormal];

UIImage *buttonImage = [UIImage imageNamed:@"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];

[audioBtn addTarget:self action:@selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;

[audioBtn release];
[button release];

我看不到导航栏中的按钮,但是如果我单击右侧(按钮应该在的位置),它会触发函数“toggleAudioPlayback”,所以唯一的问题是我没有看到按钮!

我尝试使用不同的图像,设置背景颜色,但没有任何效果...

顺便说一句,我在代码中的其他地方使用了该图像,并且我看到了它(在自定义按钮上,但不在导航栏中)。

请帮助我!

I want to add a custom button (to the right) in my navigation bar.

I have the following code in my "viewDidLoad" function :

UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:@"Play" forState:UIControlStateNormal];

UIImage *buttonImage = [UIImage imageNamed:@"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];

[audioBtn addTarget:self action:@selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;

[audioBtn release];
[button release];

I can't see the button in my navigation bar, but if I click to the right (where the button is supposed to be), it triggers the function "toggleAudioPlayback", so the only problem is that I don't see the button!

I tried with different image, setting a background color, nothing works...

By the way, I use this image somewhere else in the code, and I see it (on a custom button, but not in a navigationBar).

Help me please!

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

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

发布评论

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

评论(2

后知后觉 2024-10-28 06:58:02

让您的生活更轻松,只需使用带有自定义图像的 UIBarButtonItem 而不是 customView:

    UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"play.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toggleAudioPlayback:)];
    self.navigationItem.rightBarButtonItem = audioBtn;
    [audioBtn release];

更新

由于您已经表明您希望按钮没有边框,那么您最终需要使用 CustomView,但我已经修改了您的代码以使其工作(主要区别是框架的分配,它设置为图像的大小,但您可以将其设置为自定义大小以使图像居中)< /em>:

UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:@"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:@selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];

Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:

    UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"play.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toggleAudioPlayback:)];
    self.navigationItem.rightBarButtonItem = audioBtn;
    [audioBtn release];

UPDATE

Since you have indicated you want your button to not have a border, then you will need to use a CustomView afterall, but I have modified your code to make this work (the key difference is the assigning of the frame, which is set to the size of the image, but you could set it to a custom size to have the image centered):

UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:@"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:@selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
倒带 2024-10-28 06:58:02

尝试在 UIButton 上使用 +buttonWithType: 方法,而不是 [[UIButton alloc] init]

Instead of [[UIButton alloc] init] try the +buttonWithType: method on UIButton.

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