ios 如何向标准 UIButton 添加徽章?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4722669/
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
How can I add a badge to a standard UIButton?
提问by ohadpr
Is it possible to add a standard-looking badge to a standard UIButton
?
是否可以在标准中添加一个看起来很标准的徽章UIButton
?
If it's not supported semi-natively, what would be the simplest way to achieve this?
如果它不受半本地支持,那么实现这一目标的最简单方法是什么?
Example image:
示例图像:
回答by Dan Ray
Here's a VERY NICE class by Sascha Paulus called CustomBadge, that builds and renders custom badges using Core Graphics. They're just UIView subclasses, so you lay them out using their frame just like any other UIView subclass.
这是 Sascha Paulus 的一个非常好的类,名为CustomBadge,它使用 Core Graphics 构建和呈现自定义徽章。它们只是 UIView 子类,因此您可以像任何其他 UIView 子类一样使用它们的框架来布置它们。
I've used this library many times and always been pleased with the results. Flexible, easy to use. Totally recommend it.
我已经多次使用这个库,并且总是对结果感到满意。灵活,易于使用。完全推荐它。
回答by Carlos Ricardo
You can check for several options here: CocoaControls Badges
您可以在此处查看多个选项:CocoaControls Badges
回答by Javier Soto
The class that Apple uses is _UIBadgeView
(https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/_UIBadgeView.h), but of course, this class is private (note the underscore) and not documented.
Apple 使用的类是_UIBadgeView
(https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/_UIBadgeView.h),但当然,这个类是私有的(注意下划线) 并且没有记录。
Here's another class that implements this view with the same look and feel as Apple's while also allowing you to customize it: https://github.com/JaviSoto/JSBadgeView. The great thing about this one is that it lets you position the badge relative to another view automatically, in one of its corners.
这是另一个类,它以与 Apple 相同的外观和感觉实现此视图,同时还允许您对其进行自定义:https: //github.com/JaviSoto/JSBadgeView。关于这个的伟大之处在于它可以让您自动将徽章相对于另一个视图定位在它的一个角落中。
回答by featherless
We have a badge view in Nimbus that's super easy to use and well documented:
我们在 Nimbus 中有一个徽章视图,它非常易于使用且有据可查:
回答by Bj?rn Kaiser
I don't know how this is done out of the box and I honestly doubt that Apple built that into the SDK.
我不知道这是如何开箱即用的,老实说,我怀疑 Apple 是否将其内置到 SDK 中。
Anyway, you could create a custom view with a button on it, add the background of the badge as a UIImageView and place a label on it to hold the badge count.
无论如何,您可以创建一个带有按钮的自定义视图,将徽章的背景添加为 UIImageView 并在其上放置一个标签以保存徽章计数。
That's a quick solution, it might be better to create a custom subclass of UIButton
and add your badge stuff as a subview
这是一个快速的解决方案,最好创建一个自定义子类UIButton
并将您的徽章内容添加为子视图
回答by Eiko
I'd go a similar way as @Bj?rn Kaiser: Use the button as you like it, and then add a custom view with that badge as a subview to it - I've done it and it works well.
我会采用与@Bj?rn Kaiser 类似的方法:随心所欲地使用按钮,然后添加一个带有该徽章的自定义视图作为它的子视图 - 我已经完成了,并且效果很好。
As for the view, you can draw it as you like. You can draw it manually or use CoreAnimation and let it make the main part, i.e. theBadge.layer.cornerRadius = ...;
to give it a round shape, draw the text/number in drawRect:
or add it as a label etc.
至于视图,您可以随心所欲地绘制它。您可以手动绘制它或使用 CoreAnimation 并让它成为主要部分,即theBadge.layer.cornerRadius = ...;
给它一个圆形,绘制文本/数字drawRect:
或将其添加为标签等。