You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 15.0中adjustsImageWhenDisabled被弃用,求警告解决方法

解决iOS 15+中adjustsImageWhenDisabled废弃警告的方案

当你使用UIButtonConfiguration创建按钮时,旧的adjustsImageWhenDisabled属性会被忽略,iOS 15+官方推荐通过configurationUpdateHandler来复刻原有的禁用时图片调整效果,具体实现如下:

  • 先创建按钮的基础配置:
var buttonConfig = UIButton.Configuration.filled()
buttonConfig.image = UIImage(named: "your_normal_image")
// 按需设置其他配置(比如标题、颜色等)
let customButton = UIButton(configuration: buttonConfig)
  • 给按钮设置configurationUpdateHandler,根据启用状态调整图片样式:
customButton.configurationUpdateHandler = { button in
    guard var updatedConfig = button.configuration else { return }
    // 复刻系统默认效果:禁用时降低图片透明度(系统默认alpha是0.5,可按需调整)
    let imageAlpha: CGFloat = button.isEnabled ? 1.0 : 0.5
    updatedConfig.image = updatedConfig.image?.withTintColor(.black, alpha: imageAlpha)
    
    // 如果你需要替换成完全不同的禁用态图片,可改用下面的代码:
    // updatedConfig.image = button.isEnabled ? UIImage(named: "normal") : UIImage(named: "disabled")
    
    button.configuration = updatedConfig
}
  • 后续动态修改按钮isEnabled状态时,configurationUpdateHandler会自动触发,无需额外手动更新样式。

如果你的按钮没有使用UIButtonConfiguration,可以继续保留adjustsImageWhenDisabled属性;但只要用了新的Configuration方案,就必须通过上述方式实现禁用态的图片调整逻辑。

内容的提问来源于stack exchange,提问作者CErdtmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 12:05:18