2

Since iOS 16 there is a new feature for the ".sheet" modifier called ".presentationDetents". ".presentationDetents" has a parameter called "selection" where you can pass a Binding. You can programmatically resize the sheet with the "selection" parameter. As soon as you change the sheet size for example from PresentationDetent.medium to PresentationDetent.large right after changed the page with a "NavigationLink" the View below gets cut off:

But if I slightly move (resize) the sheet afterwards the cut off below is going to disappear:

The view hierarchy is also strange: enter image description here

If you add a delay by 0.6s for resizing the sheet, the cut off won't happen.

.onAppear {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
        currentSelection = .large
    }
}

You can find the code below:

import SwiftUI

struct ContentView: View {
    @State private var sheetIsOpened = false
    @State private var currentSelection = PresentationDetent.medium
    
    var body: some View {
        Text("Click to open a sheet")
            .padding()
            .onTapGesture {
                sheetIsOpened = true
            }
            .sheet(isPresented: $sheetIsOpened) {
                NavigationStack {
                    List {
                        ScrollView {
                            ForEach(0..<100) { index in
                                VStack {
                                    NavigationLink(destination: NavigatedView(currentSelection: $currentSelection)) {
                                        Text("I have the index: \(index)")
                                            .foregroundColor(.green)
                                    }
                                }
                                .frame(maxWidth: .infinity)
                            }
                        }
                        .padding()
                    }
                }
                .presentationDetents([.medium, .large], selection: $currentSelection)
            }
    }
}

struct NavigatedView: View {
    @Binding fileprivate var currentSelection: PresentationDetent
    
    var body: some View {
        ScrollView {
            ForEach(0..<100) { index in
                VStack {
                    Text("I'm a child and I have the index: \(index)")
                        .onAppear {
                            // DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
                                currentSelection = .large
                            // }
                        }
                }
                .frame(maxWidth: .infinity)
            }
        }
        .background(.red)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Gipfeli
  • 197
  • 3
  • 8
  • While that's odd behaviour, what's also unusual is including a vertical `ScrollView` inside a `List` (which already adds its own scroll view) so that it behaves like the table's first (and only) cell. That unusual implementation may be exercising the SwiftUI rendering subsystem in ways Apple hadn't contemplated, maybe? – ScottM Nov 30 '22 at 10:50
  • I have removed the ScrollView but the problem is still there. – Gipfeli Nov 30 '22 at 11:39

0 Answers0