20

I have a button in my code and I have a file called LogindView.swift

I cannot get the code to open another view file when clicking on the button.

Can anybody give me an example on how to do it.

In my button action I have tried to write LogindView() but i just gives me a warning. "Result of 'LogindView' initializer is unused"

    Button(action: {
            // Do action
            LogindView()
        }, label: {
            //** Label text
            Text("Logind")
                .font(.headline)
                .padding(.all)
                .foregroundColor(Color.white)
        })
        .background(Color.blue)
Jens Thomsen
  • 327
  • 1
  • 2
  • 5
  • Funny! I did enter some code. But here it goes. Button(action: { // Do action LogindView() }, label: { //** Label text Text("Logind") .font(.headline) .padding(.all) .foregroundColor(Color.white) }) .background(Color.blue) The LogindView() gives me this warning. "Result of 'LogindView' initializer is unused" – Jens Thomsen Aug 01 '19 at 16:29
  • @JensThomsen I suggest add that code to your question - here in a comment is not readable – Antonio Aug 01 '19 at 16:38
  • Added code to my question. – Jens Thomsen Aug 01 '19 at 17:54

2 Answers2

73

You essentially have 3 options to transition between views depending on your needs.


First, you can use a NavigationView. This will provide a back button and will allow the user to go back. Note that there are some bugs currently when you don't put the NavigationLink inside of a List as per https://stackoverflow.com/a/57122621/3179416

import SwiftUI

struct MasterView: View {
        var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: LoginView()) {
                    Text("Login")
                }
            }
            .navigationBarTitle(Text("Master"))
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

NavigationView


Second, you can present a modal using .sheet. This will present a modal that appears on top of the current view but it can be dismissed by the user by dragging it down.

import SwiftUI

struct MasterView: View {
    @State var isModal: Bool = false

    var body: some View {
        Button("Login") {
            self.isModal = true
        }.sheet(isPresented: $isModal, content: {
            LoginView()
        })
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

.sheet


Third, you can just use an if statement to change the current view to your Login View like so

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
            } else {
                Button("Login") {
                    self.showLoginView = true
                }
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

Changing the view without animation

If you would like to animate this, so that the transition doesn't appear so abruptly, you can also do this:

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
                    .animation(.spring())
                    .transition(.slide)
            } else {
                Button("Login") {
                    withAnimation {
                        self.showLoginView = true
                    }
                }.animation(.none)
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

Transition with animation

Zain
  • 1,569
  • 1
  • 13
  • 19
5

You can use navigation link instead button

var body: some View {
    VStack {
        Text("Title")
            .font(.headline)
        Image("myimage").clipShape(Circle())
        Text("mytext").font(.title)
        
        NavigationLink(destination: AnotherView()) {
           Image(systemName: "person.circle").imageScale(.large)
        }
  
    }
}
fiona16ti
  • 67
  • 1
  • 1