我想在UIToolbar中创建一个“后退”左箭头边框按钮。

据我所知,获得其中一个的唯一方法是将UINavigationController保留在默认设置,它使用一个用于左侧栏项。但没有办法我可以找到创建一个作为一个UIBarButtonItem,所以我不能在一个标准的UIToolbar,即使他们非常类似于UINavigationBars。

我可以用按钮图像手动创建它,但我在任何地方都找不到源图像。它们有alpha通道边缘,所以截图和剪切不会得到非常通用的结果。

对于我打算使用的每种尺寸和配色方案,除了截图之外还有什么想法吗?

更新:请停止回避问题,并建议我不应该问这个,应该使用UINavigationBar。我的应用程序是Instapaper Pro。它只显示了一个底部工具栏(以节省空间和最大化可读内容区域),我希望在底部放置一个左箭头形状的后退按钮。

告诉我,我不需要这样做不是答案,当然也不值得赏金。


当前回答

解决方案不使用png。基于这个SO答案:在iPhone TableView单元格中添加小箭头到单元格的右侧

只是水平翻转UITableViewCell !

UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 70, 20)];

// Add the disclosure
CGRect frm;
UITableViewCell *disclosure = [[UITableViewCell alloc] init];
disclosure.transform = CGAffineTransformScale(CGAffineTransformIdentity, -1, 1);
frm = self.btnBack.bounds;
disclosure.frame = CGRectMake(frm.origin.x, frm.origin.y, frm.size.width-25, frm.size.height);
disclosure.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
disclosure.userInteractionEnabled = NO;
[self.btnBack addSubview:disclosure];

// Add the label
UILabel *lbl = [[UILabel alloc] init];
frm = CGRectOffset(self.btnBack.bounds, 15, 0);
lbl.frame = frm;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text = @"BACK";
[self addSubview:lbl];

其他回答

要为UIToolbar创建一个图像,在photoshop中制作一个png,无论哪里有任何颜色,它都会把它放在白色,而当它的alpha = 0时,它就会离开它。

SDK实际上是在你所创建的图标周围设置边界,并在你不需要做任何事情的情况下将其变成白色。

看,这是我在Photoshop中为前进按钮做的(显然是把它换成后退按钮):

http://twitpic.com/1oanv

这是它在Interface Builder中出现的样子

http://twitpic.com/1oaoa

我使用了以下从http://www.teehanlax.com/blog/?p=447派生的psd

http://www.chrisandtennille.com/pictures/backbutton.psd

然后我只是创建了一个自定义的UIView,我在工具栏项的customView属性中使用。

很适合我。


编辑:正如prariehippo所指出的,maralbjo发现使用以下,简单的代码做到了这一点(需要自定义图像在捆绑)应该与这个答案相结合。这里是额外的代码:

// Creates a back button instead of default behaviour (displaying title of previous screen)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(backAction)];

tipsDetailViewController.navigationItem.leftBarButtonItem = backButton;
[backButton release];

制作一个带有箭头的UIButton(我不是设计师;),非常接近iOS 7系统的后退箭头:

标准:

Apple SD Gothic Neo

在Xcode中:

关注按钮的标题值字段(或任何其他带有文本内容的视图/控件) 打开编辑->特殊字符 选择括号组,双击'<'字符 更改字体为:Apple SD哥特式Neo,常规与所需的大小(例如20) 随你喜欢改变颜色

参考@staticVoidMan对iOS 7上类似后退的箭头的回答

警告:有报道称这将无法在iOS 6上运行。这可能只适用于较旧版本的操作系统。显然,至少有一名开发者的应用因为使用这种技巧而被拒绝。使用风险自负。使用图像(见上面的答案)可能是一个更安全的解决方案。

这可以在不添加您自己的图像文件使用sekr1t按钮类型101来获得正确的形状。对我来说,窍门是找出我可以使用initWithCustomView来创建BarButtonItem。我个人需要这个动态导航栏而不是工具栏,但我用工具栏测试了它,代码几乎是一样的:

// create button
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Back" forState:UIControlStateNormal];

// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

// add to toolbar, or to a navbar (you should only have one of these!)
[toolbar setItems:[NSArray arrayWithObject:backItem]];
navItem.leftBarButtonItem = backItem;

如果您在工具栏上执行此操作,则必须调整设置项的方式,但这取决于您的其余代码,我将其作为读者的练习。这个样例为我工作(编译和运行)。

等等,阅读HIG,不要使用未记录的特性,等等。只有六种支持的按钮类型,这不是其中之一。

Unicode方法

我认为使用unicode字符来完成工作要容易得多。你可以通过谷歌Unicode三角形或Unicode箭头来查看箭头。从iOS6开始,苹果将该字符改为带有边框的表情符号字符。为了禁用边框,我添加了0xFE0E Unicode变体选择器。

NSString *backArrowString = @"\U000025C0\U0000FE0E"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftButtonItem = backBarButtonItem;

代码块仅用于演示。它可以在任何接受NSString的按钮中工作。

要获得完整的字符列表,请搜索谷歌以获取Unicode字符和所需内容。这是黑色左指向三角形的入口。

结果