如何移除SwiftUI中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
相关产品推荐
相关产品推荐

