简体   繁体   English

在 SwiftUI 中的视图之间移动

[英]Moving between views in SwiftUI

ok so, im on on SwiftUI Xcode 11.3.1 trying to call a second view after login好的,我在 SwiftUI Xcode 11.3.1 上尝试在登录后调用第二个视图

the working design of the second view is on the file mainView.swift第二个视图的工作设计在文件 mainView.swift 中

after the login I'm calling my view like this登录后,我像这样调用我的视图

Button(action: {
                self.status =  dologin(username: self.username, password: self.password)
                if (self.status){
                    print ("log in succesfull")
                    mainMenu() // <-- return of "mainMenu" initializer is unused

                }
            })

I know you can call a.我知道你可以打电话给a。 subview like .sheet(isPresented) but this won't work for me cause is a login screen..sheet(isPresented)这样的子视图,但这对我不起作用,因为是登录屏幕。 thanks in advance.提前致谢。

You need to control views in other way in SwiftUI, you can't just push or pop view, like in UIKit.你需要在 SwiftUI 中以其他方式控制视图,你不能像 UIKit 那样只是pushpop视图。 Apple proposes NavigationView and you can use something like NavigationLink(destination: YourLoginView(), isActive: $someBindingVariable, label: Text("")) , but I really don't like to play with .navigationBarHidden(true) and .navigationBarBackButtonHidden(true) and other staff. Apple 提出了NavigationView ,你可以使用NavigationLink(destination: YourLoginView(), isActive: $someBindingVariable, label: Text("")) ,但我真的不喜欢玩.navigationBarHidden(true).navigationBarBackButtonHidden(true)和其他工作人员。 There are some options:有一些选择:

  1. Use .popover(isPresented: $needToLogin) { // login view } .使用.popover(isPresented: $needToLogin) { // login view } Either you can use .sheet .您可以使用.sheet In this case user can move your "Login view" to bottom and use "Main view" as usual:在这种情况下,用户可以将您的“登录视图”移动到底部并像往常一样使用“主视图”:
struct MovingViewsSwiftUI: View {

    @State private var needToLogin = true
    var body: some View {
        Rectangle() // your "Main view"
            .popover(isPresented: $needToLogin) {
                LoginView(needToLogin: self.$needToLogin) // will show you at the end
        }

    }

}
  1. You can use if...else statements, for example:您可以使用if...else语句,例如:
//...
var body: some View {
    if needToLogin {
        return AnyView(LoginView)
    } else {
        return AnyView(MainView)
    }
    // something else in body
}
  1. You can use ZStack and control views .opacity or .offset .您可以使用ZStack并控制视图.opacity.offset It allows you to achieve interesting animations:它允许您实现有趣的动画:
struct MovingViewsSwiftUI: View {

    @State private var needToLogin = true
    var body: some View {

        ZStack {
            LoginView(needToLogin: $needToLogin)
                .opacity(needToLogin ? 1 : 0)

            Rectangle()
                .opacity(needToLogin ? 0 : 1)

        }

    }

}

and here is the example of LoginView :这是LoginView的示例:

struct LoginView: View {

    @Binding var needToLogin: Bool
    @State private var email: String = ""
    @State private var password: String = ""

    var body: some View {
        VStack {
            TextField("enter email", text: $email)
            TextField("pass", text: $password)

            Button(action: {
                withAnimation {
                    self.needToLogin = false
                }

            }) {
                Text("Log in!")
            }
        }
        .padding()
    }
}

You can also add button in NaigationView like this.您还可以像这样在 NaigationView 中添加按钮。

 NavigationLink(destination: mainMenu()) {
                            Button(action: {

                            }) {
                              Text("Log in!")
                            }
                        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM