Liquid Glass sheet 中 .borderedProminent 与视图颜色不一致如何统一?
结论
从复现现象看,这更像是 Liquid Glass 对 sheet 内的系统颜色视图做了材质/vibrancy 适配,而 .borderedProminent 的填充仍按 tint 的普通颜色绘制。SwiftUI 公开 API 目前没有提供直接开关,用来读取玻璃适配后的系统颜色,或强制 .borderedProminent 使用该适配色。目前也没有公开文档说明这是不是官方预期行为。要保证视觉一致,通常只有两种策略:两边都用固定颜色,或放弃 .borderedProminent,自己绘制同色背景。
为什么会不一致
.borderedProminent 是系统按钮样式,按钮背景由系统样式绘制,不同于普通视图的 Color.blue 填充,因此它不会像 sheet 中系统颜色视图那样走同一套适配路径。.foregroundStyle(Color.blue)、.fill(.tint) 和 Color.blue 背景都会偏移,说明 tint 环境本身仍有自适应;.borderedProminent 最后用的却是未调整的 tint。Color(uiColor: .systemBlue) 和固定 RGB 不偏移,也说明偏移来自系统颜色在玻璃材质中的渲染,tint 值本身没有被改写。
方案一:两边固定颜色,保留 .borderedProminent
如果按钮必须保留系统 .borderedProminent 行为,最稳妥的是让图标和按钮都使用同一个非自适应颜色。例如:
private let consistentBlue = Color(uiColor: .systemBlue)
...
.background(consistentBlue, in: RoundedRectangle(cornerRadius: 14))
...
.buttonStyle(.borderedProminent)
.controlSize(.large)
.tint(consistentBlue)
这样两者都渲染成普通 systemBlue,不再随玻璃背景变化。代价是失去 Liquid Glass 的自适应效果。也可以用固定 Color(red:green:blue:),但亮/暗模式和可访问性对比度得自己处理。
方案二:两边都自适应,自定义 ButtonStyle
如果更看重玻璃自适应,也能接受放弃系统 .borderedProminent 的完整外观和交互细节,可以自定义 ButtonStyle,用和图标相同的 Color.blue 画按钮背景:
struct GlassProminentButtonStyle: ButtonStyle {
var tint: Color = .blue
func makeBody(configuration: Configuration) -> some View {
configuration.label
.font(.headline)
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
.background(
tint.opacity(configuration.isPressed ? 0.82 : 1),
in: RoundedRectangle(cornerRadius: 12)
)
}
}
Button("Action") {
}
.buttonStyle(GlassProminentButtonStyle(tint: .blue))
这种写法让按钮背景和图标一样,都是 sheet 内的普通颜色视图,因此更容易获得一致的玻璃适配。但它不等价于系统 .borderedProminent 的按压、禁用、焦点、动态字体和辅助功能表现;需要逐项补齐。
其他可选做法
- 可以试试
.buttonStyle(.glassProminent)。它是 Liquid Glass 体系下的 prominent 样式,但实测仍可能和普通Color.blue视图有轻微色差,无法保证完全一致。 - 用实心
.presentationBackground(...)或.largedetent 能让两者都变成普通systemBlue,但这会改变 sheet 的玻璃呈现,不只是改颜色。 - 采样到的 RGB 不建议硬编码(例如 19, 164, 255),因为不同背景、外观模式和设备上都会变,而且这不是公开 API。
注意事项
目前没有公开 API 能让 .borderedProminent 直接使用 Liquid Glass 适配后的系统颜色;如果必须同时满足“系统 prominent 行为”和“完全一致的玻璃适配色”,建议向 Apple Feedback 提需求,并在当前版本中根据产品取舍选择固定颜色或自定义按钮样式。
备注:内容仅供参考。
