为 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添加系统图标?
发布评论