UIBarButtonItem
has multiple icons available. Is it possible to use the icon which appears after setting its identifier to 'trash':
with an UIButton
? There is no straighforward method to do that like setting the identifier or style.
UIBarButtonItem
has multiple icons available. Is it possible to use the icon which appears after setting its identifier to 'trash':
with an UIButton
? There is no straighforward method to do that like setting the identifier or style.
All the iOS system icons can be extracted using a handy little app called (fittingly enough) iOS Artwork Extractor. I use it all the time when I want to mimic iOS system behaviors.
Download the Xcode project at:
https://github.com/0xced/iOS-Artwork-Extractor
Based on @yycking answer, i wrote an suitable Swift 4 extension:
Download the image from somewhere on the web, add it to your project and set the
UIButton
's image to the image you just downloaded.I did not find the same as Apple is using but I found this one. Simply change it's color in Pixelmator or Photoshop.
As already mentioned in the comments to @Islam Q's answer, the there presented solution might fail, if the UINavigationItem isn't currently rendered onscreen. It fails, for example, if the view controller isn't currently loaded. In fact the problem seems to be the missing layout of the UINavigationBar in these cases.
A more 'bulletproof' version would be to use a specially created UINavigationBar object just for getting the system item images. This would also make save saving and restoring of any existing UIBarButtonItems obsolete.
I've packed this into a small helper class:
LEABarButtonSystemItemImage.h:
LEABarButtonSystemItemImage.m
As an add on/fallback , the method returns an custom image, if no system item image could be retrieved. Of course these custom images must be present in the apps bundle.
The last method 'barButtonImages' was implemented just for curiosity... in the UIBarButtonItem header the member systemItem is declared to use 7 bits (0..127). Currently only 22 values are documented from UIBarButtonSystemItemDone to UIBarButtonItemSystemItemPageCurl... and in fact; I found some undocumented images starting with indexes above 100 (tested on iOS 9.3 in the 6S+ simulator) :-)
Using Quartz2D with Swift 4.2
The solutions based in an extension to extract the image from the
UIBarButtonSystemItem
don't work in iOS 11/12, so I decided to add a custom class to draw the icons without adding any .pngThis is implemented for .trash and .action that are the icons I need in my project. Feel free to add the rest.
Use it as follows (You need to conform to the
SystemIConDelegate
protocol, set the Delegate property and add the required method):The
SystemIcon
class is here. It is optimized for 30 X 30 points:for swift 4.2 (call it on main thread)
UIBarButtonSystemItem.play.image()
For Objective-C: