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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.