HomeiOS Development.accessibilityShowButtonShapes deprecated - The way to accurately deal with conflicts with .glassEffect?

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


My app helps iOS 16+ With a view to assist Glass in iOS 26, I created some customized modifiers to solely add Glass when out there.

Whereas this works high-quality generally some customers reported issues when utilizing the accessibility options: iOS Settings/Accessibility/Show & Textual content Dimension/ Button Shapes and Scale 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 many others.) is white…

I used to be in a position to @Surroundings(.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 alternative.

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

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

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


Code:

struct GlassTest: View {
    @Surroundings(.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: .middle)
        .background(.crimson)
    }
}

#Preview {
    GlassTest()
}




// ================================================================================================
// MARK: Glass Impact
// ================================================================================================
enum GlassType {
    case clear
    case common
    
    @out there(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 #out there(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 #out there(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 #out there(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

- Advertisment -
Google search engine

Most Popular

Recent Comments