17 C
Canberra
Saturday, January 24, 2026

.accessibilityShowButtonShapes deprecated – The right way to appropriately deal with conflicts with .glassEffect?


My app helps iOS 16+ So as to help Glass in iOS 26, I created some customized modifiers to solely add Glass when accessible.

Whereas this works high-quality typically some customers reported issues when utilizing the accessibility options: iOS Settings/Accessibility/Show & Textual content Measurement/ Button Shapes and Cut back Teransparancy

When these two options are enabled by glass buttons are proven with a strong white background. Which is an issue when the button content material (textual content, icon, and so on.) is white…

I used to be in a position to @Setting(.accessibilityShowButtonShapes) to make use of a distinct button content material shade when this function is used. Nonetheless this property is deprecated and there’s no substitute.

So, how I’m purported to deal with this function correcly with out realizing wether it’s used or not?

.accessibilityShowButtonShapes deprecated – The right way to appropriately deal with conflicts with .glassEffect?

This picture present the buttons beneath iOS 18 (no glass) and iOS 26 (glass accessible). With and with out accessiblity options enabled.


Code:

struct GlassTest: View {
    @Setting(.accessibilityShowButtonShapes) var buttonShapes
    @Namespace var namespace
    
    var physique: some View {
        VStack {
            HStack {
                Button(motion: { print("Whats up World") }) {
                    Picture(systemName: "star.fill")
                        .foregroundStyle(.white)
                        .padding(8)
                }
                
                Button(motion: { print("Whats up World") }) {
                    Picture(systemName: "coronary heart.fill")
                        .foregroundStyle(.white)
                        .padding(8)
                }
            }
            .glass(.clear, padding: 8, unionId: 1, namespace: namespace)
            .glassEffectContainer()
            
            
            HStack {
                Button(motion: { print("Whats up World") }) {
                    Picture(systemName: "star.fill")
                        .foregroundStyle(.white)
                        .padding(8)
                }
                .glass(.clear, padding: 8)
                
                Button(motion: { print("Whats up World") }) {
                    Picture(systemName: "coronary heart.fill")
                        .foregroundStyle(.white)
                        .padding(8)
                }
                .glass(.clear, padding: 8)
            }
        }
        .body(maxWidth: .infinity, maxHeight: .infinity, alignment: .heart)
        .background(.purple)
    }
}

#Preview {
    GlassTest()
}




// ================================================================================================
// MARK: Glass Impact
// ================================================================================================
enum GlassType {
    case clear
    case common
    
    @accessible(iOS 26, *)
    var glass: Glass {
        change self {
            case .clear:        return .clear
            case .common:      return .common
        }
    }
}

struct GlassButtonModifier: ViewModifier {
    let padding: CGFloat
    let unionId: Int?
    let namespace: Namespace.ID?
    
    func physique(content material: Content material) -> some View {
        if #accessible(iOS 26, *) {
            content material
                .padding(padding)
                .contentShape(Circle())
                .buttonStyle(.glass)
                .applyIf(unionId != nil && namespace != nil) { view in
                    view.glassEffectUnion(id: unionId!, namespace: namespace!)
                }
        } else {
            content material
        }
    }
}

struct GlassModifier: ViewModifier {
    let glassType: GlassType
    let padding: CGFloat
    let interactive: Bool
    let unionId: Int?
    let namespace: Namespace.ID?
    
    func physique(content material: Content material) -> some View {
        if #accessible(iOS 26, *) {
            content material
                .padding(padding)
                .glassEffect(interactive ? glassType.glass.interactive() : glassType.glass)
                .applyIf(unionId != nil && namespace != nil) { view in
                    view.glassEffectUnion(id: unionId!, namespace: namespace!)
                }
        } else {
            content material
        }
    }
}

struct GlassEffectContainerMofifier: ViewModifier {
    func physique(content material: Content material) -> some View {
        if #accessible(iOS 26, *) {
            GlassEffectContainer {
                content material
            }
        } else {
            content material
        }
    }
}

extension View {
    func glassEffectContainer() -> some View {
        self.modifier(GlassEffectContainerMofifier())
    }
    
    func glassButton(padding: CGFloat = 0, unionId: Int? = nil, namespace: Namespace.ID? = nil) -> some View {
        modifier(GlassButtonModifier(padding: padding, unionId: unionId, namespace: namespace))
    }
    
    func glass(_ glass: GlassType = .common, padding: CGFloat = 12, interactive: Bool = true, unionId: Int? = nil, namespace: Namespace.ID? = nil) -> some View {
        modifier(GlassModifier(glassType: glass, padding: padding, interactive: interactive, unionId: unionId, namespace: namespace))
    }
    
    
    func applyIf<Content material: View>(_ situation: Bool, modifier: (Self) -> Content material, orElse: ((Self) -> Content material)? = nil) -> some View {
        if situation {
            return AnyView(modifier(self))
        } else {
            return AnyView(self)
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles