“imageEdgeInsets”在 iOS 15.0 中已弃用

Joh*_*sen 48 ios swift

我收到警告:

'imageEdgeInsets' was deprecated in iOS 15.0
Run Code Online (Sandbox Code Playgroud)

当像这样设置 UIButton imageEdgeInsets 时:

button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
Run Code Online (Sandbox Code Playgroud)

我尝试像这样设置 imageEdgeInsets:

UIImage(named: "filter")?.withRenderingMode(.alwaysTemplate).withAlignmentRectInsets(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
Run Code Online (Sandbox Code Playgroud)

上、左、右、下都是正值或负值,没有运气。有谁知道版本的

imageEdgeInsets
Run Code Online (Sandbox Code Playgroud)

iOS 15 中没有弃用这一点吗?感谢所有帮助,亲切的问候:)

Raj*_*han 81

iOS 15 Apple 引入了 3 个新选项来控制填充和插入。

\n
    \n
  1. .titlePadding:标题和副标题标签之间的填充。
  2. \n
  3. .imagePadding:按钮\xe2\x80\x99s图像和文本之间的填充。
  4. \n
  5. .contentInsets:从按钮\xe2\x80\x99s 内容区域到其边界的填充。
  6. \n
\n

使用上述选项,您可以根据需要管理和设置按钮样式。

\n

在此输入图像描述

\n

你可以查看这篇文章了解更多。来源和图片

\n

所以你的代码应该是这样的

\n
var configuration = UIButton.Configuration.filled()\nconfiguration.title = "Title"\nconfiguration.image = UIImage(systemName: "swift")\nconfiguration.titlePadding = 10\nconfiguration.imagePadding = 10\nconfiguration.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)\n
Run Code Online (Sandbox Code Playgroud)\n

  • 惊人的!它有效,我只是无法让它与非系统映像一起使用。当我使用资产中的巨大图像时,无论 UIButton.Configuration 还是 buttonSize,它的大小都不正确。对此提供帮助将不胜感激!(希望解决方案不是填充 500 或调整图像大小) (3认同)
  • @SamuraiSteve我猜,你可能不会阻止它,但你可以在配置中将其设置为:```myButton.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer {传入in var outcoming =传入outcoming.font = UIFont(name: "MyFont ", size: 16) 返回结果 }``` (2认同)

Vis*_*kse 11

Swift 现在默认带有不同类型的按钮,您可以从 iOS 15 开始进行设置。用于UIButton.Configuration设置您的按钮。对于您的情况,您可以使用,

config.imagePadding = 5
Run Code Online (Sandbox Code Playgroud)

下面是一个例子:


var filled = UIButton.Configuration.filled()
filled.title = "Filled button"
filled.buttonSize = .large
filled.subtitle = "With subtitle even"
filled.image = UIImage(systemName: "bubble.left.fill")
filled.imagePlacement = .trailing
filled.imagePadding = 5

let button = UIButton(configuration: filled, primaryAction: nil)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

图片和代码来源于nemecek.be