Search code examples
swiftxcode11ios13

How to set iOS 13 glyphs programmatically


Starting with iOS 13, Apple is providing a bunch of glyphs which can be used "everywhere you can use an image".

You can use SF symbols to represent tasks and types of content in a variety of specific places, such as navigation bars, toolbars, tab bars, context menus, and Home Screen Quick Actions. Throughout the rest of your app, you can use a symbol everywhere you can use an image.

I therefore expected this code to set a glyph as a UIBarButtonItem's image:

shareButton = UIBarButtonItem(
            image: UIImage(named: "square.and.arrow.up"), // the name for the 'action' (Share) glyph
            style: .plain,
            target: self,
            action: #selector(mySelector))

But the app crashed with nil as the image wasn't found.
I tried appending .png to the image name, but it made no difference.

How can we set iOS 13 glyphs programmatically?

  • Note: I installed the SF Symbols app and successfully set glyphs as the images for UIBarButtonItems in Storyboard.

Solution

  • Replace:

    UIImage(named: "square.and.arrow.up")
    

    With:

    UIImage(systemName: "square.and.arrow.up")
    

    This is now documented in UIImage