Ejemplo: swiftui presenta nueva vista
// To show a modal (iOS 13 style), You just need a simple sheet with the ability to dismiss itself:structModalView:View
@Bindingvar presentedAsModal:Boolvar body: some ViewButton("dismiss")self.presentedAsModal =false// And present it like:structContentView:View
@Statevar presentingModal =falsevar body: some ViewButton("Present")self.presentingModal =true.sheet(isPresented: $presentingModal)ModalView(presentedAsModal:self.$presentingModal)// Note that I passed the presentingModal to the modal so you can dismiss it from the modal itself, but you can get rid of it.// To make it REALLY present fullscreen (Not just visually)// You need to access to the ViewController. So you need some helper containers and environment stuff:structViewControllerHolderweakvar value:UIViewController?structViewControllerKey:EnvironmentKeystaticvar defaultValue:ViewControllerHolderreturnViewControllerHolder(value:UIApplication.shared.windows.first?.rootViewController)extensionEnvironmentValuesvar viewController:UIViewController?getreturnself[ViewControllerKey.self].value setself[ViewControllerKey.self].value = newValue //Then you should use implement this extension:extensionUIViewControllerfunc present<Content:View>(style:UIModalPresentationStyle=.automatic, @ViewBuilder builder:()->Content)let toPresent =UIHostingController(rootView:AnyView(EmptyView()))
toPresent.modalPresentationStyle = style
toPresent.rootView =AnyView(builder().environment(.viewController, toPresent))self.present(toPresent, animated:true, completion:nil)// Finally you can make it fullscreen like:structContentView:View
@Environment(.viewController)privatevar viewControllerHolder:UIViewController?var body: some ViewButton("Login")self.viewControllerHolder?.present(style:.fullScreen)Text("Main")// Or any other view you like
Comentarios y puntuaciones
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)