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

如何移除SwiftUI中NavigationLink的背景色?

移除NavigationLink的灰色背景解决方案

NavigationLink默认带有按钮样式的灰色背景/点击高亮,仅设置backgroundStyle(Color.clear)无法覆盖默认样式,你可以通过修改按钮样式来彻底移除灰色部分:

  • 给NavigationLink添加.buttonStyle(PlainButtonStyle()),这会完全移除默认的按钮背景与点击高亮效果
  • iOS 15及以上版本也可以使用.buttonStyle(.borderless),效果一致

修改后的完整代码如下:

struct DesignView: View {
    @State var index: Int
    @State var selection: Int

    var body: some View {
        ZStack(alignment: .center){
            RoundedRectangle(cornerRadius: 90.0)
                .fill(Color.white)
                .frame(height: 50)
            
            NavigationLink(
                destination: GraphView(type: selection, index: index),
                label: {
                    Text("Warm Up #\(index)")
                        .font(.system(size: 20, weight: .bold, design: .rounded))
                        .foregroundColor(.black)
                })
            // 移除默认按钮样式,消除灰色背景
            .buttonStyle(PlainButtonStyle())
            .padding()
        }
    }
}

如果需要自定义点击交互效果,可以额外添加.contentShape(Rectangle())确保整个区域可点击,或者通过.simultaneousGesture自定义点击反馈。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 04:35:32