How to add an icon to a standard UIButton?

Can I add a standard icon to the UIButton standard?

If this is not supported semi-linearly, what would be the easiest way to achieve this?

Image example:

An iPhone UI button

+65
ios iphone cocoa-touch badge
Jan 18 2018-11-11T00:
source share
6 answers

Here's a VERY LOW Sascha Paulus class called CustomBadge , which creates and displays custom icons using Core Graphics. These are just subclasses of UIView, so you use them with your frame like any other subclass of UIView.

I have used this library many times and have always been pleased with the results. Flexible, easy to use. I totally recommend it.

+77
Jan 18 2018-11-11T00:
source share
โ€” -

Here you can check several options: CocoaControls Icons

+5
Mar 20 '13 at 15:32
source share

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 (reverse attention to underlining) and are not documented.

Here is another class that implements this view with the same appearance and feel as Apple, and also allows you to customize it: https://github.com/JaviSoto/JSBadgeView . The great thing about this is that it allows you to automatically position the icon in relation to another view in one of its corners.

+3
Sep 09 '13 at 22:48
source share

We have an icon in Nimbus that is very easy to use and well documented:

Nimbus badge

+2
Jun 20 '12 at 7:15
source share

I do not know how this is done out of the box, and I honestly doubt that Apple built it in the SDK.

In any case, you can create your own view using the button on it, add the background of the icon as a UIImageView and place a label on it to hold the icon counter.

This quick fix, it might be better to create a custom UIButton subclass and add your icon material as a preview

+1
Jan 18 '11 at 15:52
source share

I would do the same as @ Bjรถrn Kaiser: use the button as you like, and then add a custom view with this icon as a subroutine to it - I did it and it works fine.

As for the view, you can draw it as you wish. You can draw it by hand or use CoreAnimation and let it do the main part, i.e. theBadge.layer.cornerRadius = ...; to give it a round shape, draw text / number in drawRect: or add it as a label, etc.

+1
Jan 18 '11 at 20:27
source share



All Articles