Tuesday, January 23, 2024
HomeiOS Developmentios - Challenges with sheet presentation with `sheet(merchandise:onDismiss:content material:)` and PropertyWrapper

ios – Challenges with sheet presentation with `sheet(merchandise:onDismiss:content material:)` and PropertyWrapper


When using sheet(merchandise:onDismiss:content material:), are there particular concerns that we have to have in mind? I’ve a category, Presenter, that centrally manages all displays, and I make the most of the DynamicProperty of CustomProperty to replace UserDefaults (for simplicity, its code is omitted right here). The offered code demonstrates the proper performance of sheet(isPresented:onDismiss:content material:). Nonetheless, when incorporating sheet(merchandise:) inside the given construction, the slider fails to maneuver, though the worth is up to date, and the reset button doesn’t perform.

@propertyWrapper
personal struct CustomProperty: DynamicProperty {
    @State personal var worth : CGFloat
    
    init(worth: CGFloat) {
        self.worth = worth
    }

    var wrappedValue: CGFloat {
        get { worth }
        nonmutating set { worth = newValue }
    }
    
   var projectedValue: Binding<CGFloat> {
        .init(get: { wrappedValue }, set: { wrappedValue = $0 })
    }
}


personal struct SheetView34: View {
    @Binding var fontSize: CGFloat
    var physique: some View {
        VStack {
            Slider(worth: $fontSize, in: 20...40)
            Button("reset") {
                fontSize = 30
            }
        }
    }
}

@Observable
personal class Presenter {
    struct Sheet: Identifiable {
        let id = UUID()
        let view: AnyView
    }
    var currentSheet: Sheet?
    static let shared = Presenter()
}

struct SwiftUIView34: View {
    @State var present: Bool = false
    @Bindable personal var presenter = Presenter.shared
    @CustomProperty(worth: 30) personal var fontSize
    
    var physique: some View {
        VStack {
            Textual content("fontSize: (Int(fontSize))").daring()
            Button("present (isPresented:)") {
                present.toggle()
            }
            Button("present (merchandise:)") {
                let sheet = Presenter.Sheet(view: AnyView(SheetView34(fontSize: $fontSize)))
                Presenter.shared.currentSheet = sheet
            }

        }
        .sheet(isPresented: $present) {
           AnyView(SheetView33(fontSize: $fontSize)
            .presentationDetents([.height(200)]))
        }
        .sheet(merchandise: $presenter.currentSheet) { sheet in
            sheet.view
                .presentationDetents([.height(200)])
        }
    }
}

#Preview {
    SwiftUIView34()
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments