You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

iOS 26中带玻璃效果的Prominent样式导航栏按钮图标几乎不可见的正确着色方案咨询

iOS 26中带玻璃效果的Prominent样式导航栏按钮图标几乎不可见的正确着色方案咨询

嘿,这个坑我刚踩过!iOS 26新增的.prominent风格导航栏按钮用了Material You那套半透明毛玻璃效果,你全局设的systemGreen tint刚好和按钮的半透明背景融合在一起,图标自然就几乎看不见了。给你两个靠谱的解决办法:

方案一:快速修复——单独给按钮设置高对比度TintColor

这是最省事的方法,直接给这个done按钮单独设置一个和毛玻璃背景反差大的tint颜色,覆盖全局的systemGreen设置就行。系统的.prominent样式会自动根据这个tint调整按钮的半透明背景色,同时让图标颜色和背景形成足够对比:

// 先创建按钮
let style: UIBarButtonItem.Style = if #available(iOS 26.0, *) { .prominent } else { .done }
let doneButton = UIBarButtonItem(image: UIImage(systemName: "checkmark"), style: style, target: self, action: #selector(done))

// 关键:给按钮单独设置高对比度的tintColor
if #available(iOS 26.0, *) {
    // 推荐用label色,自动适配亮暗模式(亮模式黑/暗模式白)
    doneButton.tintColor = UIColor.label
    // 也可以用系统高对比色,比如systemBlue、systemRed
    // doneButton.tintColor = UIColor.systemBlue
}

navigationItem.rightBarButtonItem = doneButton

方案二:完全定制——用自定义UIButton包装(适合需要更多样式调整的场景)

如果你想对按钮的背景透明度、圆角大小等做更细致的调整,可以用UIButton作为自定义视图来创建UIBarButtonItem,这样能完全掌控所有视觉属性:

if #available(iOS 26.0, *) {
    // 创建自定义按钮
    let customDoneButton = UIButton(type: .system)
    customDoneButton.setImage(UIImage(systemName: "checkmark"), for: .normal)
    customDoneButton.addTarget(self, action: #selector(done), for: .touchUpInside)
    
    // 设置图标颜色(这里用systemRed做例子,可替换为任意高对比色)
    customDoneButton.tintColor = UIColor.systemRed
    
    // 启用prominent样式的毛玻璃效果
    customDoneButton.configuration = .filled()
    customDoneButton.configuration?.prominentShadow = true // 开启系统毛玻璃阴影
    customDoneButton.configuration?.cornerStyle = .capsule // 保持胶囊圆角样式
    // 也可以自定义背景透明度,比如用全局tint的半透明版本
    customDoneButton.configuration?.background.backgroundColor = UIColor.systemGreen.withAlphaComponent(0.2)
    
    // 把自定义按钮转成UIBarButtonItem
    let doneBarButton = UIBarButtonItem(customView: customDoneButton)
    navigationItem.rightBarButtonItem = doneBarButton
} else {
    // 兼容旧版本的逻辑不变
    let doneButton = UIBarButtonItem(image: UIImage(systemName: "checkmark"), style: .done, target: self, action: #selector(done))
    navigationItem.rightBarButtonItem = doneButton
}

额外提醒

别直接修改UIBarButtonItem.appearance().tintColor来全局调整,除非你想让所有导航栏按钮都换颜色——这种全局修改很容易影响其他页面的按钮样式,单独给需要调整的按钮设置tintColor才是精准的做法。

你可以先试试方案一,基本能快速搞定图标看不见的问题!

火山引擎 最新活动