如何以编程方式为UIButton添加系统图标?

编程入门 行业动态 更新时间:2024-10-27 08:27:41
本文介绍了如何以编程方式为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添加系统图标?

本文发布于:2023-07-27 05:24:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1220593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图标   方式   系统   UIButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!