gpt4 book ai didi

ios - 如何使用 Swift 在 ios 中的按钮上设置图标?

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:08 58 4
gpt4 key购买 nike

我想使用 Swift 在 IOS 中使用按钮上的图标,就像在 Android 中按钮上的图标一样。有什么办法可以不设置图片或者不设置fontAwesome图标来设置按钮上的图标吗?

最佳答案

通过 Swift 3 和 Swift 4 中的代码:

yourButton.setImage(UIImage(named: "imgName"), for: .normal)

或背景图片:

yourButton.setBackgroundImage(UIImage(named: "imgName"), for: .normal)

通过 Storyboard:

您还可以通过 Storyboard设置按钮的图标,在按钮元素的 attributes inspector 中设置 image。如果您想在图像上设置标题,请在属性检查器的 background 中设置图像

enter image description here

来自评论的问题

how do you put the image alongside the "button" text?

您可以通过代码来做到这一点,例如通过设置按钮的 imageEdgeInsets。下面的示例将图像放在文本的右侧

yourButton.setImage(UIImage(named: “imgNameWhichYouWantToSetAlongsideTheButton"), for: .normal)
yourButton.imageEdgeInsets = UIEdgeInsets(top: 3, left: 0, bottom: 0, right: -8)

关于ios - 如何使用 Swift 在 ios 中的按钮上设置图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301791/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com