简体   繁体   English

如何在 SwiftUI 中全屏显示视图?

[英]How to present a view full-screen in SwiftUI?

I worked to a login view , now I want to present the view after login, but I do not want the user to have the possibility to return to the login view .我工作到登录view ,现在我想在登录后显示该view ,但我不希望用户有可能返回到登录view In UIkit I used present() , but it seems in SwiftUI presentation(_ modal: Modal?) the view does not take the entire screen.UIkit我使用了present() ,但似乎在SwiftUI presentation(_ modal: Modal?)view并没有占据整个屏幕。 Navigation also isn't an option. Navigation也不是一种选择。

Thank you!谢谢!

I do not want the user to have the possibility to return to the login view我不希望用户有可能返回到登录视图

In that case you shouldn't be presenting away from the login view but replacing it entirely.在这种情况下,您不应该离开登录视图,而是完全替换它。

You could do this by conditionally building the login view or the "app view".您可以通过有条件地构建登录视图或“应用程序视图”来做到这一点。

Something like this...像这样的东西...

// create the full screen login view
struct LoginView: View {
    // ...
}

//create the full screen app veiw
struct AppView: View {
    // ...
}

// create the view that swaps between them
struct StartView: View {
    @EnvironmentObject var isLoggedIn: Bool // you might not want to use this specifically.

    var body: some View {
        isLoggedIn ? AppView() : LoginView()
    }
}

By using a pattern like this you are not presenting or navigating away from the login view but you are replacing it entirely so it is no longer in the view hierarchy at all.通过使用这样的模式,您不会呈现或导航离开登录视图,而是完全替换它,因此它根本不再位于视图层次结构中。

This makes sure that the user cannot navigate back to the login screen.这可确保用户无法导航回登录屏幕。

Equally... by using an @EnvironmentObject like this you can edit it later (to sign out) and your app will automatically be taken back to the login screen.同样......通过使用这样的@EnvironmentObject您可以稍后对其进行编辑(以退出),您的应用程序将自动返回到登录屏幕。

Encapsulate the body in a Group to eliminate compiler errors:将 body 封装在一个 Group 中以消除编译器错误:

struct StartView: View {

@EnvironmentObject var userAuth: UserAuth

var body: some View {
    Group {
        if userAuth.isLoggedin {
            AppView()
        } else {
            LoginView()
        }

    }
}
struct ContentView: View {
    @EnvironmentObject var userAuth: UserAuth 
    var body: some View {
        if !userAuth.isLoggedin {
            return AnyView(LoginView())
        } else {
            return AnyView(HomeView())
        }
    }
}

I made this extension for myself.我为自己做了这个扩展。 Any feedback/ideas are welcome.欢迎任何反馈/想法。 :) :)

https://github.com/klemenkosir/SwiftUI-FullModal https://github.com/klemenkosir/SwiftUI-FullModal

Usage用法

struct ContentView: View {

    @State var isPresented: Bool = false

    var body: some View {
        NavigationView {
            Button(action: {
                self.isPresented.toggle()
            }) {
                Text("Present")
            }
            .navigationBarTitle("Some title")
        }
        .present($isPresented, view: ModalView(isPresented: $isPresented))
    }
}

struct ModalView: View {

    @Binding var isPresented: Bool

    var body: some View {
        Button(action: {
            self.isPresented.toggle()
        }) {
            Text("Dismiss")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在动作表中呈现动作扩展而不是全屏模态视图? - How to present an action extension in an action sheet rather than full-screen modal view? 是否可以在全屏UICollectionViewCell上显示GADInterstitial? - Is it possible to present GADInterstitial on a full-screen UICollectionViewCell? 使用 Xcode 11 时,SwiftUI 视图以小 window 而不是全屏呈现 - SwiftUI view being rendered in small window instead of full-screen when using Xcode 11 自动调整遮罩和全屏视图 - Autoresizing mask and full-screen view 如何在点击按钮时全屏显示.sheet() 以便将 go 显示到 SwiftUI 中的下一个视图? - How to present .sheet() full screen when a button is tapped in order for it to go to the next view in SwiftUI? 当非全屏时,如何获取MPMoviePlayerController的视图以在旋转时调整大小 - How to get MPMoviePlayerController's view to resize on rotate when it is not full-screen 使用UIImagePickerController拍摄视频和picker'view不能全屏显示吗? - Using UIImagePickerController to take video and picker'view not full-screen? 检查视图是在Popover还是全屏显示 - Check whether View is shown in Popover or in full-screen 如何在SpriteKit中应用全屏SKEffectNode进行后期处理 - How to apply full-screen SKEffectNode for post-processing in SpriteKit 如何单击全屏 UIView 后面的按钮? - How do I click a button behind a full-screen UIView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM