1

I'm trying to achieve having a GlassEffectContainer with some buttons and the select button has a red bottom border.

My only issue is that the glass effect isn't being applied on the background where the border is added.

Glass Container

struct GroupedGlassBorder: View {
 
 @State var selected: Int = 1
 
 var body: some View {
 GlassEffectContainer {
 HStack {
 BorderButton(title: "One", num: 1, selected: $selected)
 BorderButton(title: "Two", num: 2, selected: $selected)
 BorderButton(title: "Three", num: 3, selected: $selected)
 }
 }
 .glassEffect()
 }
}
struct BorderButton: View {
 
 var title: String
 var num: Int
 
 @Binding var selected: Int
 
 var body: some View {
 Button {
 self.selected = num
 } label: {
 Text(title)
 .padding(12)
 }
 .background(alignment: .bottom) {
 Capsule()
 .frame(height: 2)
 .foregroundStyle(selected == num ? .red : .clear)
 }
 
 }
}
asked Dec 3, 2025 at 18:23
1
  • 1
    @BenzyNeez if I add .buttonStyle(.glass) the effect is in each button and I want to have the GlassEffectContainer Commented Dec 3, 2025 at 18:51

1 Answer 1

2

The red line is in the background of the selected button, but the buttons are shown above the container, not below it. So even if the container has a glass effect, it only works for content below it, not for the content on top.

I would suggest the following changes:

  • Show the red line (capsule) behind the HStack.
  • Use .matchedGeometryEffect to match the line to the selected button. Each button should have a separate id, the line should be matched to the button that corresponds to the current selection.
  • Add .buttonStyle(.glass(.clear)) to the buttons.
  • Group the buttons inside the container by applying .glassEffectUnion. The same union id should be used for all three buttons.
  • To prevent the red line from showing at the ends, apply a clip shape to the glass container.
  • A change in selection can also be animated by applying an .animation modifier.

To make the red line more prominent, I found that it works well to add padding of 1 point below each button. This way, half the height of the red capsule is shown behind the button, the lower half is just below the button.

Here is the updated example to show it all working:

struct GroupedGlassBorder: View {
 @State var selected: Int = 1
 @Namespace private var ns
 var body: some View {
 GlassEffectContainer {
 HStack {
 BorderButton(title: "One", num: 1, selected: $selected)
 .matchedGeometryEffect(id: 1, in: ns)
 .glassEffectUnion(id: 0, namespace: ns)
 BorderButton(title: "Two", num: 2, selected: $selected)
 .matchedGeometryEffect(id: 2, in: ns)
 .glassEffectUnion(id: 0, namespace: ns)
 BorderButton(title: "Three", num: 3, selected: $selected)
 .matchedGeometryEffect(id: 3, in: ns)
 .glassEffectUnion(id: 0, namespace: ns)
 }
 .background {
 Capsule()
 .frame(height: 2, alignment: .bottom)
 .foregroundStyle(.red)
 .matchedGeometryEffect(id: selected, in: ns, isSource: false)
 }
 }
 .glassEffect(.clear)
 .clipShape(.capsule)
 .animation(.default, value: selected)
 }
}
struct BorderButton: View {
 var title: String
 var num: Int
 @Binding var selected: Int
 var body: some View {
 Button {
 self.selected = num
 } label: {
 Text(title)
 .padding(12)
 }
 .buttonStyle(.glass(.clear))
 .padding(.bottom, 1)
 }
}

Animation

answered Dec 3, 2025 at 19:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.