UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: PresentationDetents

Forums > SwiftUI

Can someone tell me why my presentation detents aren't working. When I use the following code, the sheet doesn't display half way up like I think it should. I've tried moving the sheet off the button thinking that was the issue, however, it still presented the sheet as normal. Can anyone tell me why the detents aren't doing what I'd expect. I'm using Xcode 15 Beta 8

struct SectionListView: View {
    // MARK: - SwiftData
    @Query(sort: \SectionSD.name, order: .forward, animation: .easeInOut) var sections: [SectionSD]

    // MARK: - State
    @State private var addSection = false

    var body: some View {
        List {
            Button(action: { addSection.toggle() }, label: {
                HStack {
                    Image(systemName: "plus")
                        .foregroundColor(Color.accentClr)
                        .bold()
                    Text("Add Section")
                        .textViewModifier(for: .body, weight: .medium, color: .primary)
                }
            })
            .sheet(isPresented: $addSection) {
                NavigationStack {
                    AddSectionView(section: nil)
                        .presentationDetents([.medium])
                }
            }

            ForEach(sections) { section in
                NavigationLink(value: section) {
                    SectionRowView(section: section)
                }
            }
        }
        .navigationTitle("Sections")
        .navigationDestination(for: SectionSD.self) { section in
            AddSectionView(section: section)
        }
    }
}

Thanks, Mark

2      

I cant really verify this as im on my phone but have you tried moving the presentationDetent view modifier down a line?

NavigationStack {
                    AddSectionView(section: nil)
           }
           .presentationDetents([.medium])

Apologies, if the formatting is off.

2      

@Kaiman you are right. Here some test code

struct ContentView: View {
    @State private var addSection = false

    var body: some View {
        List {
            Button(action: { addSection.toggle() }, label: {
                HStack {
                    Image(systemName: "plus")
                        .foregroundColor(Color.blue)
                        .bold()
                    Text("Add Section")
                }
            })
            .sheet(isPresented: $addSection) {
                NavigationStack {
                    Text("AddSectionView")
                }
                .presentationDetents([.medium])
            }
        }
        .navigationTitle("Sections")
    }
}

2      

@Kaiman & @NigelGee,

I hadn't tried that and that was the anwser. Not sure why that works and you can't put it on the view itself. However, it's working. So thank you both for replying I do appreciate it.

Thanks,

2      

Thank you for helping me out as well, you made my day. :)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.