UIButton作为UINavigationbar按钮 [英] UIButton as UINavigationbar button
问题描述
我有一个Tab栏应用程序。并在其中一个选项卡栏中有一个导航控制器。
我试图设置一个图像作为右侧导航栏的UIButton。
UIButton * refreshButton = [UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:@refresh_icon.png] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshSection)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
// [rightButton setImage:[UIImage imageNamed:@refresh_icon.png]]; <<不工作
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
但图片不显示。我得到一个看不见的按钮。
编辑:
我想让按钮样式为纯色或背景透明,以便只显示图片。因此,我使用的是UIbutton而不是直接使用UIBarButton。
您不必创建 UIButton
,并将其用作 UIBarButtonItem
中的自定义视图。您可以直接使用以下图片创建 UIBarButtonItem
:
UIImage * myImage = [UIImage imageNamed:@myImage.png];
UIBarButtonItem * button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:@selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
-
更新 c。使用 UIImageView
作为自定义视图 UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@UIImageView; UIBarButtonItem :
myImage.png]];
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
-
更新
尝试了最后一种方法,虽然这看起来像是你想要的,我似乎不能处理水龙头。
最终更新
我已经在问题中得到了你的初始代码&运行。图片未显示的原因是因为您尚未设置按钮的边框。一旦设置框架,图像就会显示在导航栏中。
这是我的代码段,我使用&测试:
UIImage * myImage = [UIImage imageNamed:@paw.png];
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0,0.0,myImage.size.width,myImage.size.height);
[myButton addTarget:self action:@selector(tapped :) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
注意:我设置了 showsTouchWhenHighlighted
属性 UIButton
在按下时可以高亮显示按钮。
I have a Tab bar application. and in one of the tab bars I have a navigation contorller. Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:@"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT: I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton
and use it as custom view in the UIBarButtonItem
. You can create a UIBarButtonItem
directly with an image:
UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:@selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView
as custom view in the UIBarButtonItem
:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:@"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted
property of the UIButton
to visually hightlight the button when it is pressed.
这篇关于UIButton作为UINavigationbar按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!