Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

ios - SwiftUI - Changing GraphicalDatePickerStyle DatePicker's visibility causes memory leak

I have an app that has a datepicker (GraphicalDatePickerStyle). It hidden by default. When you tap the view that contains it, it becomes visible.

DateTimePicker

struct DateTimePicker<Content: View>: View {
    @Binding var selection: Date
    
    @State private var isDatePickerVisible: Bool = false
        
    private var displayedComponents: DatePickerComponents
    
    private var content: (_ isVisible: Bool) -> Content
    
    
    init(selection: Binding<Date>,
         displayedComponents: DatePickerComponents = [.date],
         content: @escaping (_ isVisible: Bool) -> Content) {
        self._selection = selection
        self.content = content
        self.displayedComponents = displayedComponents
    }
    

    var body: some View {
        VStack(alignment: .center) {
            self.content(isDatePickerVisible)
                .onTapGesture {
                    withAnimation {
                        self.isDatePickerVisible.toggle()
                    }
                }
            if isDatePickerVisible {
                VStack {
                    DatePicker("",
                               selection: $selection,
                               displayedComponents: self.displayedComponents)
                        .labelsHidden()
                        .datePickerStyle(GraphicalDatePickerStyle())
                }
            }
        }
    }
}

ContentView that contains DateTimePicker

struct ContentView: View {

    @State var selection = Date()

    var body: some View {
        ScrollView(.vertical) {
            DateTimePicker(selection: $selection) { _ in
                HStack {
                    Text("Date ")
                    Spacer()
                    Text("(selection)")
                }
            }
        }
    }
}

There are a few memory leaks if you show and hide DateTimePicker. I tried a lot but could not fix it.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...