ios 使用后退按钮样式创建 UIBarButtonItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14605425/
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
Create UIBarButtonItem with back button style
提问by Fry
I'm looking for a way to create programmatically an UIBarButtonItem
that looks like a back button of UINavigationBar
.
Apparently seems like that the back button appears only after a push on the UINavigationController
.
我正在寻找一种以编程方式创建UIBarButtonItem
看起来像UINavigationBar
. 显然,后退按钮似乎只有在按下UINavigationController
.
So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.
所以我只能插入一个“取消”样式的按钮。但我的目标是创建一个具有“新项目”样式的按钮。
Ideas ?
想法?
采纳答案by holex
the short answer is you cannot do it.
简短的回答是你不能这样做。
you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar
.
您可以保存图像并将图像放在该位置,但后退按钮由UINavigationBar
.
回答by Guo Luchuan
I think you need to use image. and then set the image be the buttons backgroundImage. such as :
我认为你需要使用图像。然后将图像设置为按钮背景图像。如 :
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:@"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:@"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
回答by Ali Abbas
Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
看看这个答案:Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
It gives you a psd file with image that you need.
它为您提供了一个带有您需要的图像的 psd 文件。
回答by steve
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:@"Back"];
这个对我有用:
[self.navigationItem.leftBarButtonItem setTitle:@"Back"];
(custom text with back arrow)
(带有后退箭头的自定义文本)