My purpose is to make a offered sheet match to it is content material on iOS 16 and up, utilizing the presentationDetent .peak and passing within the peak calculated from geometry reader.
I do not need the second backside sheet to be draggable, it ought to stay it is measurement calculated from the content material peak.
The difficulty is that I am getting a unique peak again from the geometry proxy which could be very annoying, and ending up with completely different outcomes relying on the variations.
When operating under code on simulator iPhone 15 on iOS 17.4 the peak printed shall be:
Peak: 40.666666666666664
When operating on iPhone 15 utilizing iOS 18 the peak printed shall be:
Peak: 0.0
I’ve tried utilizing a desire key, however then when the sheet is dragged, the sheets peak will after all be up to date. I’ve additionally tried utilizing .background as an alternative of overlay, job as an alternative of onAppear, and many others.
Anybody has an thought the right way to repair this, or one other strategy I might attempt?
That is the minimal reproducible instance code:
import SwiftUI
@major
struct experimentingApp: App {
var physique: some Scene {
WindowGroup {
ContentView(viewModel: .init())
}
}
}
enum ActiveSheet: Identifiable {
case sheetOne, sheetTwo
var id: String {
swap self {
case .sheetOne:
"sheet-one"
case .sheetTwo:
"sheet-two"
}
}
}
ultimate class ViewModel: ObservableObject {
@Printed var activeSheet: ActiveSheet? = .sheetOne
}
struct ContentView: View {
@ObservedObject var viewModel: ViewModel
@State var sheetHeight: CGFloat = .zero
var physique: some View {
Textual content("Guardian view")
.sheet(isPresented: isPresented(for: .sheetOne), content material: {
VStack {
Textual content("Sheet one")
Button(motion: { viewModel.activeSheet = .sheetTwo}, label: { Textual content("Subsequent")})
}
})
.sheet(isPresented: isPresented(for: .sheetTwo), content material: {
VStack(spacing: 0) {
Textual content("Sheet two")
Textual content("Sheet two")
}
.overlay(content material: {
GeometryReader { proxy in
Coloration.clear
.ignoresSafeArea(edges: .all)
.onAppear {
print("Peak: (proxy.measurement.peak)")
sheetHeight = proxy.measurement.peak
}
}
})
.presentationDetents([.height(sheetHeight)])
})
}
}
extension ContentView {
func isPresented(for sheet: ActiveSheet) -> Binding {
Binding(
get: { self.viewModel.activeSheet == sheet },
set: { if !$0 { self.viewModel.activeSheet = nil } }
)
}
}