在 UIToolbar 上创建左箭头按钮(如 UINavigationBar 的“后退”样式)
我想在 UIToolbar
中创建一个“后退”左箭头边框按钮。
据我所知,获得其中之一的唯一方法是将 UINavigationController 保留为默认设置,并且它对左栏项目使用一个。 但我无法将其创建为 UIBarButtonItem
,因此我无法在标准 UIToolbar
中创建一个,即使它们与 非常相似>UINavigationBar
。
我可以使用按钮图像手动创建它,但我无法在任何地方找到源图像。 它们具有 Alpha 通道边缘,因此屏幕截图和剪切不会获得非常通用的结果。
除了对我打算使用的每种尺寸和配色方案进行屏幕截图之外,还有什么想法吗?
更新:请停止回避这个问题并建议我不应该问这个问题而应该使用UINavigationBar
。 我的应用程序是 Instapaper Pro。 它只显示一个底部工具栏(以节省空间并最大化可读内容区域),我希望在底部放置一个左箭头形状的后退按钮。
告诉我不需要这样做不是答案,而且当然不值得赏金。
I'd love to create a "back" left-arrow-bezel button in a UIToolbar
.
As far as I can tell, the only way to get one of these is to leave UINavigationController
at default settings and it uses one for the left bar item. But there's no way I can find to create one as a UIBarButtonItem
, so I can't make one in a standard UIToolbar
, even though they're very similar to UINavigationBar
s.
I could manually create it with button images, but I can't find the source images anywhere. They have alpha-channel edges, so screenshotting and cutting won't get very versatile results.
Any ideas beyond screenshotting for every size and color scheme I intend to use?
Update: PLEASE STOP dodging the question and suggesting that I shouldn't be asking this and should be using UINavigationBar
. My app is Instapaper Pro. It shows only a bottom toolbar (to save space and maximize readable content area), and I wish to put a left-arrow-shaped Back button in the bottom.
Telling me that I shouldn't need to do this is not an answer and certainly doesn't deserve a bounty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
你为什么做这个? 如果您想要看起来像导航栏的东西,请使用 UINavigationBar。
工具栏具有与其相关的特定视觉样式。 iPhone 的人机界面指南指出:
然后,它给出了几个没有文本的大致方形图标的视觉示例。 我强烈建议您关注 HIG 的动态。
Why are you doing this? If you want something that looks like a navigation bar, use UINavigationBar.
Toolbars have specific visual style associated with them. The Human Interface Guidelines for the iPhone state:
It then gives several visual examples of roughly square icons with no text. I would urge you to follow the HIG on this.
我使用了从 http://www.teehanlax.com/blog/?p=447" 导出的以下 psd p=447
http://www.chrisandtennille.com/pictures/backbutton.psd< /a>
然后,我创建了一个自定义 UIView,在工具栏项的 customView 属性中使用它。
对我来说效果很好。
编辑: 正如PrairieHippo所指出的,maralbjo发现使用以下简单的代码就可以达到目的(需要捆绑中的自定义图像)应该是结合这个答案。 所以这里是额外的代码:
I used the following psd that I derived from http://www.teehanlax.com/blog/?p=447
http://www.chrisandtennille.com/pictures/backbutton.psd
I then just created a custom UIView that I use in the customView property of the toolbar item.
Works well for me.
Edit: As pointed out by PrairieHippo, maralbjo found that using the following, simple code did the trick (requires custom image in bundle) should be combined with this answer. So here is additional code:
Unicode 方法
我认为仅使用 unicode 字符来完成工作要容易得多。 您可以通过谷歌搜索 Unicode 三角形 或 < a href="http://www.fileformat.info/info/unicode/block/arrows/list.htm" rel="nofollow noreferrer">Unicode 箭头。 从iOS6开始,苹果将字符更改为带有边框的表情符号字符。 要禁用边框,我添加了 0xFE0E Unicode 变体选择器。
该代码块仅用于演示。 它适用于任何接受 NSString 的按钮。
有关字符的完整列表,请在 Google 中搜索 Unicode 字符和您想要的内容。 这是黑色左指三角形的条目。
结果
The Unicode Method
I think it is much easier to just use a unicode character to get the job done. You can look through arrows by googling either Unicode Triangles or Unicode Arrows. Starting with iOS6 Apple changed the character to be an emoji character with a border. To disable the border I add the 0xFE0E Unicode Variation Selector.
The code block is just for the demo. It would work in any button that accepts an NSString.
For a full list of characters search Google for Unicode character and what you want. Here is the entry for Black Left-Pointing Triangle.
Result
警告:有报告称这不适用于 iOS 6。这可能仅适用于旧版本的操作系统。 显然,至少有一个开发人员的应用程序因使用此技巧而被拒绝(请参阅评论)。 使用风险自负。 使用图像(参见上面的答案)可能是一个更安全的解决方案。
无需使用 sekr1t 按钮类型 101 添加您自己的图像文件即可完成此操作以获得正确的形状。 对我来说,诀窍是弄清楚我可以使用
initWithCustomView
来创建BarButtonItem
。 我个人需要动态导航栏而不是工具栏,但我用工具栏测试了它,代码几乎相同:如果您在工具栏上执行此操作,则必须进行调整如何设置这些项目,但这取决于代码的其余部分,我将其作为练习留给读者。 :P 这个示例对我有用(编译并运行)。
等等,阅读 HIG,不要使用未记录的功能,等等。 只有六种受支持的按钮类型,这不是其中之一。
WARNING: There are reports that this will not work on iOS 6. This might only work on older versions of the OS. Evidently at least one dev has had their app rejected for using this trick (see the comments). Use at your own risk. Using an image (see answer above) might be a safer solution.
This can be done without adding in your own image files using sekr1t button type 101 to get the correct shape. For me the trick was figuring out that I could use
initWithCustomView
to create theBarButtonItem
. I personally needed this for a dynamic navbar rather than atoolbar
, but I tested it with a toolbar and the code is nearly the same:If you're doing this on a toolbar you'll have to tweak how you set the items, but that depends on the rest of your code and I leave that as an exercise for the reader. :P This sample worked for me (compiled & run).
Blah blah, read the HIG, don't use undocumented features, and all that. There's only six supported button types and this isn't one of them.
首先,您必须找到后退按钮的图像。 我使用了一个名为 Extractor 的不错的应用程序,它可以从 iPhone 中提取所有图形。
在iOS7中,我设法检索名为
UINavigationBarBackIndicatorDefault
的图像,它是黑色的,因为我需要红色色调我使用Gimp将颜色更改为红色。< /strike>编辑:
正如 btate 在他的评论中提到的,没有必要使用图像编辑器更改颜色。 下面的代码应该可以解决这个问题:
然后我创建了一个视图,其中包含带有该箭头的 imageView、带有自定义文本的标签,并且在视图顶部有一个带有操作的按钮。 然后我添加了一个简单的动画(淡入淡出和平移)。
以下代码模拟后退按钮的行为(包括动画)。
编辑:
在我看来,更好的方法是使用手势识别器,而不是添加按钮。
First of all you have to find an image of the back button. I used a nice app called Extractor that extracts all the graphics from iPhone.
In iOS7 I managed to retrieve the image called
UINavigationBarBackIndicatorDefault
and it was in black colour, since I needed a red tintI change the colour to red using Gimp.EDIT:
As was mentioned by btate in his comment, there is no need to change the color with the image editor. The following code should do the trick:
Then I created a view that contains an imageView with that arrow, a label with the custom text and on top of the view I have a button with an action. Then I added a simple animation (fading and translation).
The following code simulates the behaviour of the back button including animation.
EDIT:
Instead of adding the button, in my opinion the better approach is to use a gesture recognizer.
我不确定这是否可行,但您可以尝试使用默认设置创建一个
UINavigationController
来创建按钮,在导航控制器的子视图层次结构中找到该按钮,调用removeFromSuperview 上,销毁导航控制器,然后将按钮添加为工具栏的子视图。 您可能还需要在调用
removeFromSuperview
之前保留
和按钮(然后在将其添加为工具栏的子视图后释放
它)以避免它在此过程中被释放。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, callremoveFromSuperview
on it, destroy the navigation controller, and then add the button as a subview of your toolbar. You may also need toretain
and the button before callingremoveFromSuperview
(and thenrelease
it after adding it as subview of your toolbar) to avoid it being deallocated during the process.要使带有箭头的 UIButton 非常接近(我不是设计师;)iOS 7 系统后退箭头:
标准:
Apple SD Gothic Neo
在 Xcode 中:
参考 @staticVoidMan 的回答 iOS 7 上类似后退的箭头
To make a UIButton with an arrow pretty close (I'm not a designer ;) to the iOS 7 system back arrow:
Standard:
Apple SD Gothic Neo
In Xcode:
Ref @staticVoidMan's answer to Back-like arrow on iOS 7
如果您不想费心处理图像文件,可以使用以下代码在 UIView 子类中绘制箭头形状:
其中箭头视图与宽度 6.0 和高度 10.0 成比例
If you don't want to bother with image files, the arrow shape can be drawn in a UIView subclass with the following code:
where the arrow view is proportional to a width of 6.0 and a height of 10.0
对我有用。 当我有更多选项卡然后可以放在选项卡栏上时,我使用了这个,并且从“更多”推送的视图控制器覆盖了其 viewDidLoad 中的 leftBarButtonItem。
Works for me. I used this when I had more tabs then could fit on the tab bar, and a view controller pushed from the "More" overrode the leftBarButtonItem in its viewDidLoad.
您可以通过从 UIKit ${SDKROOT}/System/Library/Frameworks/UIKit.framework/Other.artwork 中的 Other.artwork 中提取源图像来找到源图像。 改装社区有一些用于提取它们的工具,此处。 提取图像后,您可以编写一些代码以根据需要重新着色并将其设置为按钮图像。 无论你是否真的可以运送这样的东西(因为你正在嵌入派生的艺术品)可能有点冒险,所以也许你想和律师谈谈。
You can find the source images by extracting them from Other.artwork in UIKit ${SDKROOT}/System/Library/Frameworks/UIKit.framework/Other.artwork. The modding community has some tools for extracting them, here. Once you extract the image you can write some code to recolor it as necessary and set it as the button image. Whether or not you can actually ship such a thing (since you are embedding derived artwork) might be a little dicey, so maybe you want to talk to a lawyer.
Three20 库有一种方法可以做到这一点:
The Three20 library has a way to do this:
我发现使用以下简单的代码就可以解决问题(需要捆绑中的自定义图像):
I found that using the following, simple code did the trick (requires custom image in bundle):
这是我在搜索所有这些解决方案和其他解决方案后最终所做的事情。 它使用从 UIKit 库存图像中提取的可拉伸 png。 这样您就可以将文本设置为您喜欢的任何内容
Here's what I ended up doing after searching through all these solutions and others. It uses a stretchable png's extracted from the UIKit stock images. This way you can set the text to whatever you liek
使用 Xcode 5.x 中的 Interface Builder 可以轻松实现
使用对象库中的工具栏和栏按钮项
按钮的 < strong>属性检查器编辑图像部分
带有您的后退按钮图像
完成!
It's easy to do with Interface Builder in Xcode 5.x
use Toolbar and Bar Button Item from Object library
in button's Attributes inspector edit Image section
with your back button image
Done!
Swift 5.2 Xcode 11.4
Apple 符号 chevron.left 现在允许使用更优雅的解决方案来制作自定义按钮。 我已尽可能接近地匹配尺寸和间距。
这可以作为
UIToolbarItem
或UINavigationItem
实现,如果您想翻转按钮并使箭头指向右侧:
使用名为 "chevron.right" 的 Apple 符号
将以下代码添加到
CustomBackButton
类中:Swift 5.2 Xcode 11.4
The Apple Symbol chevron.left now allows a more elegant solution to make a custom button. I have matched the size and spacing as close as possible.
This can be implemented either as a
UIToolbarItem
, or aUINavigationItem
If you want to flip the button and have the arrow pointing to the Right:
Use Apple Symbol named "chevron.right"
Add the following code to the
CustomBackButton
class:我试图做同样的事情,但我希望后退按钮位于导航栏中。 (我实际上需要一个后退按钮,它的作用不仅仅是返回,所以我必须使用 leftBarButtonItem 属性)。 我尝试了 AndrewS 的建议,但在导航栏中,它看起来不像应有的样子,因为 UIButton 有点被转换为 UIBarButtonItem。
但我找到了解决这个问题的方法。 我实际上只是在 UIButton 下放置了一个 UIView 并为 UIBarButtonItem 设置了 customView。 这是代码,如果有人需要的话:
I was trying to do the same thing, but I wanted the back button to be in the navigation bar. (I actually needed a back button, that would do more than only going back, so I had to use the leftBarButtonItem property). I tried what AndrewS suggested, but in the navigation bar it wouldn't look the way it should, as the UIButton was kind of casted to a UIBarButtonItem.
But I found a way to work around this. I actually just put a UIView under the UIButton and set the customView for the UIBarButtonItem. Here is the code, if somebody needs it:
要为 UIToolbar 创建图像,请在 Photoshop 中制作一个 png,只要有任何颜色,它就会将其设置为白色,如果 alpha = 0,则将其保留。
SDK 实际上将边框放在您制作的图标周围,并将其变成白色,而无需您执行任何操作。
看,这就是我在 Photoshop 中为前进按钮制作的内容(显然将其替换为后退按钮):
http://twitpic .com/1oanv
这就是 Interface Builder 中的样子
http://twitpic.com/1oaoa< /a>
To create an image for the UIToolbar, make a png in photoshop and WHERE EVER there is ANY colour it puts it white, and where it's alpha = 0 then it leaves it alone.
The SDK actually put's the border around the icon you have made and turns it white without you having to do anything.
See, this is what I made in Photoshop for my forward button (obviously swap it around for back button):
http://twitpic.com/1oanv
and this is what it appeared like in Interface Builder
http://twitpic.com/1oaoa
不使用 png 的解决方案。 基于这个答案:将小箭头添加到 iPhone TableView 单元格中单元格的右侧
只需水平翻转 UITableViewCell 即可!
Solution WITHOUT using a png. Based on this SO answer: Adding the little arrow to the right side of a cell in an iPhone TableView Cell
Just flipping the UITableViewCell horizontally!
如果您想避免自己绘制它,您可以使用未记录的类:具有样式 1 的 UINavigationButton。当然,这可能会阻止您的应用程序被批准......
/约翰
If you want to avoid drawing it yourself, you could use the undocumented class: UINavigationButton with style 1. This could, of course, stop your application from being approved...
/John
好吧,您不必为每种尺寸使用不同的按钮,您可以使用
[UIImagestretchableImageWithLeftCapWidth:topCapHeight:]
,但我唯一发现的是自定义图像。Well, you don't have to have a different button for every size, you can use
[UIImage stretchableImageWithLeftCapWidth:topCapHeight:]
, but the only thing I've found is custom images.我遇到了类似的问题,并推出了一个库PButton。 示例是类似后退导航按钮的按钮,可以像自定义按钮一样在任何地方使用。
像这样的事情:
I had a similar problem, and come out one library PButton. And the sample is the back navigation button like button, which can be used anywhere just like a customized button.
Something like this:
Swift 3 中的示例,右上角有上一个和下一个按钮。
Example in Swift 3, with a previous and a next button in the top right.
迅速
Swift
尝试这个。 我确信您不需要后退按钮图像来创建这样的图像。
这就是你所要做的:)
Try this. I am sure you do not need a back button image to create one such.
That's all you have to do :)