how to fullscreen when use sheet in ipad SwiftUI

i can't use model.fullscreen in swiftUI

Replies

At present there isn't a simple way to do this in SwiftUI, but there are ways of obtaining this sort of feature. You'd ultimately need to engage in some shenanigans to locate a suitable

UIViewController
from which to present the sheet, then wrap your sheet view inside a
UIHostingController
to be passed to
UIViewController.present(_:animated:completion:)
.


Here's a quick example which compiles and probably works, though I've not tested it yet:


import UIKit
import SwiftUI
import Combine

#if os(iOS) || targetEnvironment(macCatalyst)
fileprivate func rootController() -> UIViewController? {
    let scenes = UIApplication.shared.connectedScenes.compactMap {
        $0 as? UIWindowScene
    }

    guard !scenes.isEmpty else { return nil }
    for scene in scenes {
        guard let root = scene.windows.first?.rootViewController else {
            continue
        }
        return root
    }
    return nil
}

fileprivate struct _SpecialSheetPseudoView: View {
    @Binding var isPresented: Bool
    @Boxed private var presentedController: UIHostingController? = nil
    var style: UIModalPresentationStyle
    var sheet: () -> SheetContent
    var content: ViewContent

    var body: some View {
        if isPresented {
            presentSheet()
        }
        else {
            dismissSheet()
        }

        return content
    }

    func presentSheet() {
        guard presentedController == nil else { return }
        guard let controller = rootController() else { return }

        let presented = UIHostingController(rootView: sheet())
        self.presentedController = presented
        controller.present(presented, animated: true, completion: nil)
    }

    func dismissSheet() {
        guard let controller = presentedController else { return }
        presentedController = nil
        controller.dismiss(animated: true, completion: nil)
    }
}

@available(iOS 13.0, tvOS 13.0, *)
@available(OSX, unavailable)
@available(watchOS, unavailable)
extension View {
    public func sheet(
        isPresented: Binding,
        style: UIModalPresentationStyle,
        @ViewBuilder content: @escaping () -> Content
    ) -> some View {
        _SpecialSheetPseudoView(isPresented: isPresented, style: style,
                                sheet: content, content: self)
    }
}
#endif
https://developer.apple.com/documentation/scenekit/sceneview/3607780-fullscreencover


or using this fullScreenCover in iOS 14 😂