如何以编程方式为UIButton添加系统图标? [英] How to add system icons for a UIButton programmatically?
问题描述
为 UIButton
添加自定义图像或背景很容易,但是似乎没有编程方式为 UIButton
,我知道它可以应用于导航栏按钮,我不需要,我想将其应用于简单的 UIButton
,有什么提示吗?
对于Swift 5,语法为:
button.setImage(UIImage(systemName:"search"),用于:.normal)
您还可以通过添加 SymbolConfiguration
:
让boldConfig = UIImage.SymbolConfiguration(weight:.bold)让boldSearch = UIImage(systemName:"search",withConfiguration:boldConfig)button.setImage(boldSearch,for:.normal)
请参阅:
It's easy to add a custom image or a background for a UIButton
, but there seems to be no programmatic way to set one of the following default iOS icons for a UIButton
, I know it can be applied to navigation bar buttons, I don't need that, I want to apply it to a simple UIButton
, any hints?
For Swift 5 the syntax is:
button.setImage(UIImage(systemName: "search"), for: .normal)
You can also set the weight of the icon by adding SymbolConfiguration
:
let boldConfig = UIImage.SymbolConfiguration(weight: .bold)
let boldSearch = UIImage(systemName: "search", withConfiguration: boldConfig)
button.setImage(boldSearch, for: .normal)
See: Apple documentation for available names (API column) or go to InterfaceBuilder, select UIButton and in Attributes Inspector select Image which will give you list of all available icons.
这篇关于如何以编程方式为UIButton添加系统图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!