如何使用 UIButton.Configuration 让 UIButton 图像根据按钮大小动态调整和调整大小

How to have UIButton image using UIButton.Configuration adapt and resize dynamically to the button size

提问人:psykocouac 提问时间:8/15/2023 最后编辑:psykocouac 更新时间:11/5/2023 访问量:526

问:

我想使用 UIButton.Configuration 在应用程序中创建和配置 UIKit 按钮。

我不能让按钮中的图像根据按钮本身的大小调整大小,从而导致图像在按钮边界之外展开。

在配置之前,我在按钮属性上使用。.scaleAspectFitimageViewcontentMode

import UIKit

class ViewController: UIViewController {

    let button1 = {
        let filterImageUrl = Bundle.main.url(forResource: "filter@2x", withExtension: "png")!
        let filterImage = UIImage(contentsOfFile: filterImageUrl.absoluteURL.path)

        var configuration = UIButton.Configuration.filled()
        configuration.image = filterImage
        configuration.imagePadding = 5
        configuration.cornerStyle = .medium
        configuration.baseBackgroundColor = .darkGray
        configuration.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)


        let button = UIButton(configuration: configuration)
        button.imageView?.contentMode = .scaleAspectFit
        button.translatesAutoresizingMaskIntoConstraints = false

        return button
    }()

    let button2 = {
        var configuration = UIButton.Configuration.filled()
        configuration.image = UIImage(systemName: "line.3.horizontal.decrease.circle")
        configuration.imagePadding = 5
        configuration.cornerStyle = .medium
        configuration.baseBackgroundColor = .darkGray

        let button = UIButton(configuration: configuration)
        button.setPreferredSymbolConfiguration(UIImage.SymbolConfiguration(scale: UIImage.SymbolScale.small), forImageIn: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false

        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(button1)
        self.view.addSubview(button2)

        let constraint = [
            button1.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            button1.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
            button1.widthAnchor.constraint(equalToConstant: 30),
            button1.heightAnchor.constraint(equalToConstant: 30),
            button2.topAnchor.constraint(equalTo: button1.bottomAnchor, constant: 10),
            button2.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            button2.widthAnchor.constraint(equalToConstant: 20),
            button2.heightAnchor.constraint(equalToConstant: 20)
        ]
        NSLayoutConstraint.activate(constraint)
    }
}

结果是


Images in the buttons expands outside them

我尝试使用约束,包括针对封闭 UIButton 的约束。imageView

还探讨了 UIImage.Configuration

我还希望配置强制在按钮内调整图像大小contentInsets

Swift UIKIT UIBuint

评论

0赞 matt 8/15/2023
不清楚目标是什么(以及目标与您的屏幕截图有何不同,这在我看来很好)。请澄清。
0赞 psykocouac 8/15/2023
感谢您@matt的反馈。我不太习惯问问题。正如您在屏幕截图中看到的,按钮内的图像不会缩放以适合其内部,也不会缩放以适应其内部,也没有 .正如我所提到的,我希望配置强制在按钮内调整图像大小。我更新了屏幕截图以使其更清晰。imagePaddingcontentInsetsUIButton.ConfigurationcontentInsets
0赞 matt 8/15/2023
好吧,第一点,你还不明白是什么。这是关于图像和标题之间的空间。但是没有标题,所以无关紧要。但也许你只是在绝望中玩耍。:)imagePadding
0赞 psykocouac 8/15/2023
你是对的,“绝望”是个好词。其实我更关注.我错误地认为它会迫使所有内容(包括图像)缩小。在使用之前,完成了这项工作。我现在使用配置,因为在 iOS 15 中已被弃用contentInsetsUIButton.Configuration.scaleAspectFitcontentInsetscontentEdgeInsets

答:

2赞 matt 8/15/2023 #1

您绝对正确,图像没有调整大小以适合按钮;反之亦然。让图像加上配置,为您调整按钮的大小。下面是一个松散地基于您的代码的示例(来自 ):contentInsetsviewDidLoad

let button = {
    var configuration = UIButton.Configuration.filled()
    configuration.image = UIImage(systemName: "line.3.horizontal.decrease.circle")!
        .applyingSymbolConfiguration(.init(pointSize: 100))
    configuration.cornerStyle = .medium
    configuration.baseBackgroundColor = .darkGray
    configuration.contentInsets = .zero
    let button = UIButton(configuration: configuration)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

self.view.addSubview(button)

let constraints = [
    button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
    button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
]

NSLayoutConstraint.activate(constraints)

要改变“边框”的数量(填充?边距?),请更改该值。contentInsets

评论

0赞 psykocouac 8/15/2023
谢谢。使用和玩弄参数,我可以实现我的目标。但它只适用于符号图像......如何对自定义和/或资产图像有类似的行为?.applyingSymbolConfiguration(.init(pointSize: 100))pointSize
0赞 matt 8/15/2023
您只需以所需的大小重新绘制图像即可。这很容易,但这不是你问的,如何做到这一点已经在这里解释了很多次。重点只是从正确大小的图像开始,而不是期望按钮为您调整大小。
0赞 psykocouac 8/15/2023
再次感谢你。就 API 而言,这有点令人失望,因为我们想修复按钮的大小并让图像相应地缩放。使用遗留 API 是可能的,您可以引导我阅读以下内容:首先使用适当的图像或在将其添加到图像之前调整其大小。
-1赞 Yarik 11/5/2023 #2

对我来说,以下工作是有效的

self.configuration?.image = yourImage.resizedToFit(in: CGSize(width: 16, height: 16))

您现在可以使用调整大小的图像。不幸的是,除了这个,我没有找到任何其他方法可以为 UIImage 做到这一点。

评论

3赞 Jens Schwarzer 11/7/2023
我看起来你有扩展 🤓UIImage