完全移至其他视图,不允许在SwiftUI中返回 [英] Completely move to other view and don't allow to go back in SwiftUI

查看:105
本文介绍了完全移至其他视图,不允许在SwiftUI中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SwiftUI开发一个具有两个视图的简单iOS应用:LogInView()HomeView().

I'm developing a simple iOS app with SwiftUI with two views: a LogInView() and a HomeView().

我想要的非常简单:当用户单击LogInView()上的登录按钮时,我希望该应用隐藏LogInView()并全屏显示HomeView(),<强烈,而不是模态,并且不允许用户返回.

What I want is really simple: when the user clicks on the Login Button on LogInView() I want the app to hide the LogInView() and show the HomeView(), full screen, not like a modal and without allowing the user to go back.

这可以通过Swift和UIKit中的Storyboards轻松完成,有没有办法通过SwiftUI做到这一点?

This could be easily done with Storyboards in Swift and UIKit, is there any way to do this with SwiftUI?

感谢您的帮助. 预先感谢.

Any help is appreciated. Thanks in advance.

我的代码:

LogInView :

struct LogInView: View {
    var body: some View {
        VStack {
            Text("Welcome to Mamoot!")
                .font(.largeTitle)
                .fontWeight(.heavy)
            Text("We are glad to have you here.")
            Text("Please log in with your Mastodon or Twitter account to continue.")
                .multilineTextAlignment(.center)
                .lineLimit(4)
                .padding()
            Spacer()
            FloatingTextField(title: "Username", placeholder: "Username", width: 300, type: "Username")
            FloatingTextField(title: "Password", placeholder: "Password", width: 300, type: "password")
                .padding(.top, -50)
            Spacer()
            ZStack {
                Button(action: { /* go to HomeView() */ }) {
                    Text("Log in")
                        .foregroundColor(Color.white)
                        .bold()
                        .shadow(color: .red, radius: 10)
                }
                .padding(.leading, 140)
                    .padding(.trailing, 140)
                    .padding(.top, 15)
                    .padding(.bottom, 15)
                    .background(Color.red)
                    .cornerRadius(10)
            }
            .padding(.bottom)
        }
    }
}

HomeView :

struct HomeView: View {
    var body: some View {
        Text("Home Page")
    }
}

推荐答案

更新:我有一些时间来更新答案并添加过渡.请注意,我更改了按VStack分组",否则过渡将无法进行.

Update: I got some time to update the answer, and add a transition. Note that I changed Group by VStack, otherwise the transition does not work.

您可以在withAnimation呼叫(按钮关闭)中更改持续时间.

You can alter the duration in the withAnimationcall (button closure).

我还在您的按钮中移动了一些修饰符,因此整个过程都是可插入的".否则,仅点击按钮上的文字将触发操作.

I also moved some modifiers in your button, so the whole thing is "tappable". Otherwise, only tapping on the text of the button would trigger the action.

您可以使用ObservedObjectEnvironmentObjectBinding.这是ObservedObject的示例:

You can use an ObservedObject, an EnvironmentObject or a Binding. Here's an example with ObservedObject:

import SwiftUI

class Model: ObservableObject {
    @Published var loggedIn = false
}

struct ContentView: View {
    @ObservedObject var model = Model()

    var body: some View {
        VStack {
            if model.loggedIn {
                HomeView().transition(.opacity)
            } else {
                LogInView(model: model).transition(.opacity)
            }
        }
    }
}

struct HomeView: View {
    var body: some View {
        Text("Home Page")
    }
}

struct LogInView: View {
    @ObservedObject var model: Model

    var body: some View {
        VStack {
            Text("Welcome to Mamoot!")
                .font(.largeTitle)
                .fontWeight(.heavy)
            Text("We are glad to have you here.")
            Text("Please log in with your Mastodon or Twitter account to continue.")
                .multilineTextAlignment(.center)
                .lineLimit(4)
                .padding()
            Spacer()
            //            FloatingTextField(title: "Username", placeholder: "Username", width: 300, type: "Username")
            //            FloatingTextField(title: "Password", placeholder: "Password", width: 300, type: "password")
            //                .padding(.top, -50)
            Spacer()
            ZStack {
                Button(action: {
                    withAnimation(.easeInOut(duration: 1.0)) {
                        self.model.loggedIn = true
                    }
                }) {
                    Text("Log in")
                        .foregroundColor(Color.white)
                        .bold()
                        .shadow(color: .red, radius: 10)
                        // moved modifiers here, so the whole button is tappable
                        .padding(.leading, 140)
                        .padding(.trailing, 140)
                        .padding(.top, 15)
                        .padding(.bottom, 15)
                        .background(Color.red)
                        .cornerRadius(10)
                }
            }
            .padding(.bottom)
        }
    }
}

这篇关于完全移至其他视图,不允许在SwiftUI中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆