Giter Club home page Giter Club logo

Comments (6)

aheze avatar aheze commented on May 19, 2024 1

Ah, I see. When you set present to true, all the popovers inside your ForEach will show, since they're all linked to the same property.

@State var present = false /// you have 1 single property for all the popovers!

Here's a simplified example of what you're doing currently.

struct ContentView: View {
    @State var present = false

    var body: some View {
        VStack(spacing: 80) {
            ForEach(0..<3) { index in
                Button {
                    present = true /// when you set this to `true`, all the popovers will present
                } label: {
                    Text("Button #\(index)")
                }
                .popover(present: $present) { /// all of the popovers use the same property
                    Text("Popover #\(index)")
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(16)
                }
            }
        }
    }
}
All popovers present at the same time, no matter which one was tapped.

Instead, since you have multiple popovers, you'll need to use the .popover(selection:tag:attributes:view:) modifier.

struct ContentView: View {
    @State var selection: String? /// keeps track of which popover is being presented.

    var body: some View {
        VStack(spacing: 80) {
            ForEach(0..<3) { index in
                Button {
                    selection = "\(index)" /// set `selection` to the current iteration's tag
                } label: {
                    Text("Button #\(index)")
                }
                
                /// will be presented when `selection` == `tag`
                .popover(selection: $selection, tag: "\(index)") {
                    Text("Popover #\(index)")
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(16)
                }
            }
        }
    }
}
Each button presents its own popover when tapped.

More info if you're interested

  • Popovers behaves similar to SwiftUI's sheet inside a ForEach.
  • Unlike SwiftUI's sheet, multiple popovers can linked to the same @State. They will just all present at the same time, which may or may not be what you want.
  • Meanwhile, if you try linking multiple SwiftUI sheets to the same @State, it will glitch out.
  • .popover(selection:tag:attributes:view:) comes with some other benefits, including support for animating between popovers!

from popovers.

wisepmlin avatar wisepmlin commented on May 19, 2024 1

First of all, your toolkit is great! Thanks for the open source
I've written them to the child view, and the new struct has received the independent properties, so there's no duplication

from popovers.

aheze avatar aheze commented on May 19, 2024

Looks like a state re-rendering problem: see here. You might need to move everything in your popover into a separate View.

.popover(present: $present) {
    PopoverView(sk_current: $sk_current)
}

/// ...

struct PopoverView: View {
    @Binding var sk_current: WhateverSK_CurrentIs
    
    var body: some View {
        VStack(spacing: CGFloat.bl_4.double) {
                            Text("📅 日历")
                                .modifier(SK_16(textColor: Color.t_p_c, weight: .bold))
                            DatePicker("",
                                       selection: $sk_current.selectedDate, displayedComponents: .date)
                                .datePickerStyle(GraphicalDatePickerStyle())
                                .accentColor(.blue_c)
                        }
                        .frame(maxWidth: 320)
                        .padding(.top, CGFloat.bl_4.custom(num: 5))
                        .padding(.horizontal, CGFloat.bl_4.custom(num: 5))
                        .background(.bar)
                        .cornerRadius(12)
                        .shadow(radius: 20)
                        .scaleEffect(expanding ? 0.95 : 1)
    }
}

from popovers.

aheze avatar aheze commented on May 19, 2024

@wisepmlin if that doesn't work, can you show a image of what you're getting currently? Thanks.

from popovers.

wisepmlin avatar wisepmlin commented on May 19, 2024

IMG_E87299B10598-1

from popovers.

aheze avatar aheze commented on May 19, 2024

Thanks! Yeah that would work too, glad you found a solution.

from popovers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.