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

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

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

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

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

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


当前回答

在Xcode 5.x中使用Interface Builder很容易做到

使用对象库中的工具栏和栏按钮项 在按钮的属性检查器编辑图像部分 你的后退按钮图像

完成了!

其他回答

您可以通过从Other中提取源图像来找到它们。UIKit ${SDKROOT}/System/Library/Frameworks/UIKit.framework/Other.artwork。modding社区有一些提取它们的工具。一旦你提取图像,你可以写一些代码来根据需要重新着色,并将其设置为按钮图像。你是否真的可以发布这样的东西(因为你正在嵌入派生的艺术品)可能有点冒险,所以你可能需要和律师谈谈。

如果你想避免自己绘制它,你可以使用没有文档的类:风格1的UINavigationButton。当然,这可能会阻止你的申请被批准。 /约翰

I'm not sure if this would work, but you could try creating a UINavigationController with the default settings to create the button, find the button in the navigation controller's subview hierarchy, call removeFromSuperview on it, destroy the navigation controller, and then add the button as a subview of your toolbar. You may also need to retain and the button before calling removeFromSuperview (and then release it after adding it as subview of your toolbar) to avoid it being deallocated during the process.

Swift 3中的例子,右上方有一个previous和next按钮。

let prevButtonItem = UIBarButtonItem(title: "\u{25C0}", style: .plain, target: self, action: #selector(prevButtonTapped))
let nextButtonItem = UIBarButtonItem(title: "\u{25B6}", style: .plain, target: self, action: #selector(nextButtonTapped))
self.navigationItem.rightBarButtonItems = [nextButtonItem, prevButtonItem]

我试图做同样的事情,但我希望后退按钮在导航栏。(我实际上需要一个返回按钮,这将做的不仅仅是返回,所以我必须使用leftBarButtonItem属性)。我尝试了AndrewS建议的,但在导航栏中,它不会看起来像它应该的样子,因为UIButton有点被强制转换为UIBarButtonItem。

但我找到了一个解决办法。我只是在UIButton下面放了一个UIView并为UIBarButtonItem设置了customView。下面是代码,如果有人需要的话:

// initialize button and button view
UIButton *backButton = [UIButton buttonWithType:101];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButton.frame.size.width, backButton.frame.size.height)];

[backButton addTarget:self action:@selector(backButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButtonView addSubview:backButton];

// set buttonview as custom view for bar button item
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButtonItem;

// push item to navigationbar items
[self.navigationController.navigationBar setItems:[NSArray arrayWithObject:backButtonItem]];