看不到我的导航栏自定义按钮,但它是可点击的
我想在导航栏中添加一个自定义按钮(右侧)。
我的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让您的生活更轻松,只需使用带有自定义图像的 UIBarButtonItem 而不是 customView:
更新
由于您已经表明您希望按钮没有边框,那么您最终需要使用 CustomView,但我已经修改了您的代码以使其工作(主要区别是框架的分配,它设置为图像的大小,但您可以将其设置为自定义大小以使图像居中)< /em>:
Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:
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 上使用
+buttonWithType:
方法,而不是[[UIButton alloc] init]
。Instead of
[[UIButton alloc] init]
try the+buttonWithType:
method on UIButton.