gpt4 book ai didi

ios - SetBackgroundImage 太大 UIButton swift

转载 作者:行者123 更新时间:2023-11-29 02:22:01 26 4
gpt4 key购买 nike

我正在尝试在 UIButton 上的文本顶部获取图像。我已将图像作为背景图像放在 UIButton 上并设置 VerticalAlignment,图像现在位于文本上方 :)像这样:

button.imageView?.image
button.setBackgroundImage(image, forState: UIControlState.Normal)
button.titleLabel?.font = UIFont(name: "Cantarell", size: 14)
button.setTitle("Test", forState: UIControlState.Normal)
button.contentVerticalAlignment = UIControlContentVerticalAlignment.Bottom

唯一的问题是图像现在填满了整个按钮,它看起来很垃圾而且呈 block 状。我曾尝试使用 Photoshop 裁剪图像,但它仍然填充按钮并且看起来呈 block 状。我可以在 swift 中设置 backgroundImage 帧大小吗?

所以图像最终是这样的:

enter image description here

因此图像需要更小且 block 状。我已经在 Photoshop 中裁剪了它,但没有用。只是让它更 block 状。我可以为按钮背景图像的大小设置 .frame 吗?

最佳答案

您可以先在 Storyboard上拖动一个 View (而不是 ImageView)。

之后,将 ImageView 和标签拖到 View 中。现在您可以使用 align 和 pin 将 ImageView 和 Label 放在 View 中的正确位置。

关于ios - SetBackgroundImage 太大 UIButton swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029657/

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