当每一侧的填充需要不同时,如何修复 iOS 15.0 中已弃用的 ImageEdgeInsets?

问题描述 投票:0回答:1

因此

imageEdgeInsets
已在
iOS 15
中被贬低。现在代码如下所示:

class MyButton: UIButton {
    init(title: String) {
        super.init(frame: .zero)
        
        setTitle(title, for: .normal)
        setTitleColor(.white, for: .normal)
        backgroundColor = .blue
        layer.cornerRadius = 10
        
        setImage(UIImage(systemName: "heart.fill"), for: .normal)
        
        // This is depricated
        imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 40)
        
        configuration?.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)
        
        addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func buttonTapped() {
        print("Button tapped!")
    }
}

我一直在寻找解决方案,并在这里找到了一个:

“imageEdgeInsets”在 iOS 15.0 中已弃用

显然我们应该使用

configuration?.imagePadding
来代替。因此,我们应该使用
configuration?.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)
,而不是
configuration?.imagePadding = 0

我想你已经可以发现这个问题了:

configuration?.imagePadding
只需要一个
CGFloat
所以如果你想在不同的侧面有不同的填充,就像这样:
NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)
你运气不好。

那么我如何摆脱弃用,同时为各方保留不同的填充?

*** 注意*** 问题与

titleEdgeInsets

相同

此外,使用

configuration?.imagePadding
后,它似乎完全没有任何作用,而
NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)
就像一个魅力。

ios swift uibutton deprecated
1个回答
0
投票

替换是图像

placement
的组合

configuration?.imagePlacement = .leading

以及图像和文字之间的距离

configuration?.imagePadding = 12.0
© www.soinside.com 2019 - 2024. All rights reserved.