【问题标题】:set image at center of background with inner space arround a button将图像设置在背景中心,按钮周围有内部空间
【发布时间】:2018-07-06 11:48:27
【问题描述】:

我已经为按钮设置了背景图像并将layer.cornerRadius 应用于该按钮,这样按钮就像一个圆圈,但是图像是从角落切割的,所以我如何将图像放置在一个中心按钮,这样当我应用 layer.cornerRadius 时不应该从角落剪切图像,有没有办法让我可以从内侧到按钮框架留出空间。

这是我尝试过的:

myCheckBoxButton.setBackgroundImage(#imageLiteral(resourceName: "check-square"), for: .normal)
myCheckBoxButton.frame = CGRect(x: 120, y: 10, width: 40, height: 40)
myCheckBoxButton.backgroundColor = .blue
myCheckBoxButton.layer.cornerRadius = 20
myCheckBoxButton.showsTouchWhenHighlighted = true
myCheckName.text = "Checkbox"
myCheckName.textColor = .gray
myCheckName.frame = CGRect(x: 100 , y: 60, width: 80, height: 15)

在 aleksmutlu 回答后更新

一切正常,但图像不是白色透明的,看起来是蓝色的

更新完成

将按钮类型添加为自定义

let myCheckBoxButton = UIButton(type: .custom)

【问题讨论】:

  • 按钮下的uiimage怎么样?您将有更多选择以这种方式调整图像大小。
  • 按钮似乎不会将背景图像缩放到按钮大小。您应该尝试使用所需大小为 40x40 像素的缩放图像。

标签: ios swift uibutton uiimage


【解决方案1】:

您可以将图像设置为按钮的内容并为其添加一些插图,而不是设置背景图像。

myCheckBoxButton.setImage(#imageLiteral(resourceName: "check-square"), for: .normal)
myCheckBoxButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

【讨论】:

  • 更新的问题是正确的,但图像不是白色的,它看起来是蓝色的?任何想法
  • 您应该将按钮类型设置为“自定义”
  • 请写下答案
  • 我正在以编程方式创建按钮!我该如何分配??
  • 谢谢let myCheckBoxButton = UIButton(type: .custom)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多