M_Khater
M_Khater

Reputation: 109

SwiftUI Sheet presentationDetents ignores the specified detents

When I build and run the project in Xcode 16.2 and rapidly opening and dismissing a sheet via scroll-down multiple times, the sheet eventually ignores the specified presentationDetents([.medium]) and appears at .large size instead.

What I expect to happen: The sheet appears with specified .medium detents only when rapidly opening and dismissing a sheet via scroll-down multiple times.

import SwiftUI

struct ContentView: View {
    @State var isPresented = false
    
    var body: some View {
        VStack {
            Button("Show") {
                isPresented = true
            }
        }
        .padding()
        .sheet(isPresented: $isPresented, content: {
            Text("Sheet")
                .presentationDetents([.medium])
        })
    }
}

Issue Enviroment:

NOTE: when I build and run the same projcet without any change in Xcode 15.4 all work's fine.

Upvotes: 1

Views: 87

Answers (1)

M_Khater
M_Khater

Reputation: 109

I found this thread.

I made a workaround to solve this issue by updating sheet detents from UISheetPresentationController of Top ViewController

import SwiftUI

struct ContentView: View {
    @State var isPresented = false
    
    var body: some View {
        VStack {
            Button("Show") {
                isPresented = true
            }
        }
        .padding()
        .sheet(isPresented: $isPresented, content: {
            Text("Sheet")
                .presentationDetents([.medium])
                .onAppear(perform: updateDetentsFromTopController)
        })
    }
    
    func updateDetentsFromTopController() {
        // topController is Current ViewController appear to the user
        guard
            let sheetPresentationController = UIApplication.shared.topController?.sheetPresentationController,
            sheetPresentationController.detents == [.large()]
        else { return }

        sheetPresentationController.detents = [.medium()]
    }
}

Upvotes: 0

Related Questions