如何以编程方式为UIButton添加系统图标? [英] How to add system icons for a UIButton programmatically?

查看:92
本文介绍了如何以编程方式为UIButton添加系统图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆