How to set iOS 13 glyphs programmatically [duplicate]

夙愿已清 提交于 2020-01-04 07:53:50

问题


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.

回答1:


Replace:

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

With:

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

This is now documented in UIImage



来源:https://stackoverflow.com/questions/56623835/how-to-set-ios-13-glyphs-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!