【问题标题】:How to resize an image inside an UIButton programmatically?如何以编程方式调整 UIButton 内的图像大小?
【发布时间】:2017-03-23 16:26:09
【问题描述】:

我有这个 UIButton 和一个适合的图像。 我不希望图像占据按钮内的所有空间,而只是占据中心的一小部分,但如果我调整按钮的大小,它也会调整图像的大小。 我该怎么做,有没有一个选项可以独立于 UIButton 的大小来设置我想要的任何尺寸? 谢谢!

【问题讨论】:

  • 您是在问如何通过 Interface Builder 或通过代码来做到这一点?

标签: ios swift xcode swift3 xcode8


【解决方案1】:

这可以通过以下方式通过代码完成:

    let imageSize:CGSize = CGSize(width: 20, height: 20)

    let button:UIButton = UIButton(type: UIButton.ButtonType.custom)
    button.frame = CGRect(x: 200, y: 200, width: 60, height: 60)
    button.backgroundColor = UIColor.yellow
    button.setImage(UIImage(named: "chat.png"), for: UIControl.State.normal)

    // The below line will give you what you want
    button.imageEdgeInsets = UIEdgeInsets(
        top: (button.frame.size.height - imageSize.height) / 2,
        left: (button.frame.size.width - imageSize.width) / 2,
        bottom: (button.frame.size.height - imageSize.height) / 2,
        right: (button.frame.size.width - imageSize.width) / 2)

    self.view.addSubview(button)

这样,你就可以实现你想要的。

【讨论】:

  • 像魅力一样工作。谢谢
【解决方案2】:

您可以尝试图片视图插图。每个 UIButton 都有一个属性 imageView。

在 Swift 3 中,您可以这样做:

//let button = UIButton()
button.imageView?.backgroundColor = UIColor.red
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

红色背景只是让你知道发生了什么变化

【讨论】:

    【解决方案3】:

    在我将 contentHorizo​​ntalAlignment 和 contentVerticalAlignment 都设置为 .fill 之前,我无法调整按钮的 imageView 大小。然后使用 imageEdgeInsets 我重新定位了图像。

    let button = UIButton()
    let image = UIImage(systemName: "bag.badge.plus")
    button.setImage(image, for: .normal)
    button.contentHorizontalAlignment = .fill
    button.contentVerticalAlignment = .fill
    button.imageEdgeInsets = UIEdgeInsets(top: 6, left: 6, bottom: 10, right: 10)
    

    结果:

    【讨论】:

      【解决方案4】:

      我会这样做:

      UIButton 只是 UIView。您可以简单地添加带有设置图像的UIImageView,然后在UIButton 上调用addSubview

      【讨论】:

      • 感谢大家,但这对我很有帮助!
      • userInteractionEnabled 对于 UIImageView 默认设置为 false
      • 我不敢相信我从来没有对此感到陌生,现在我不必为插入而苦苦挣扎了哈哈!谢谢
      • 举个例子会很有用。
      【解决方案5】:

      考虑到 KVISH 在我实施之前所说的话,它按预期工作。我发布这个是因为 Houman 要求举个例子。

      //grab the image using the name of the pic
      var image = UIImage(named: "picture")
      
      //set the size for the image
      image = image?.resize(toWidth: 18)
      image = image?.resize(toHeight: 18)
      
      //set the image to the button
      buttonName.setImage(image, for: UIControlState.normal)
      
      //adjust the position
      buttonName.imageEdgeInsets = UIEdgeInsetsMake(8,16,9,0)
      

      【讨论】:

      • 这应该是正确的,对我来说只是更改 imageEdgeInsets 并不会使图像变小或变大。
      【解决方案6】:

      这些可以通过将 imageEdgeInsets 添加到 UIButton 来实现。

      在swift4.2中

        button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
      

      【讨论】:

      • 这个答案与上述答案有何不同?
      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多