Sheet content doesn't stay centered when programmatically changing detent
Which platform is most relevant for your report?
iOS
Which technology does your report involve?
SwiftUI
What type of feedback are you reporting?
Incorrect/Unexpected Behavior
What build does the issue occur on?
iOS 17.4 Seed 3 (21E5200d)
Where does the issue occur?
On device
Run the given code.
Drag the sheet up and down, and notice the button always stays centered.
Now toggle the selected detent, and notice the button jumps and is no longer always centered.
I would expect the behavior whether dragging or programmatically setting the selected detent to be the same.
Demo
Workaround
Unknown
import SwiftUI
struct ContentView: View {
@State private var presented = true
@State private var detent: PresentationDetent = .large
var body: some View {
Button("Present") {
presented = true
}
.sheet(isPresented: $presented) {
Button("Toggle detent") {
if detent == .large {
detent = .medium
} else {
detent = .large
}
}
.presentationDetents([.medium, .large], selection: $detent)
}
}
}
#Preview {
ContentView()
}