xcode UIButton 作为 UINavigationbar 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3828781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton as UINavigationbar button
提问by Rupert
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 设置为导航栏的右侧按钮。
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.
编辑:我希望按钮样式为纯色或背景为透明,以便仅显示图像。因此我使用 UIbutton 而不是直接使用 UIBarButton。
回答by Yannick Compernol
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:
您不必创建一个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];
--
——
Updated, according to comments
根据评论更新
Use a UIImageView
as custom view in the UIBarButtonItem
:
在 中使用UIImageView
作为自定义视图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.
注意:我已将 的showsTouchWhenHighlighted
属性设置为在UIButton
按下按钮时在视觉上突出显示按钮。
回答by Rupert
I ended up doing this
我最终这样做了
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.
它现在工作正常,但我不确定这是否是正确的做法。