0

code

Hello, I want to navigate between windows using a button but not use a NavigationLink. It looks ugly. this is my code

import SwiftUI

struct ContentView: View {

    var body: some View {
        Button(action: action()){
            Text("Hola")
                .font(.largeTitle)
                .frame(width: 100, height: 100)
            .background(Color.red)
        }

    }
}
Seungjun
  • 874
  • 9
  • 21

1 Answers1

0

You can use an empty NavigationLink and bind your navigation flag or destination to your Button.

struct FirstView: View {
    
    @State var navigationFlag = false
    
    var body: some View {
        NavigationView {
            VStack {
                Text("First View")
                
                Button(action: {
                    self.navigationFlag = true
                }, label: {
                    Text("navigate")
                })
                
                NavigationLink(destination: SecondView(),
                               isActive: self.$navigationFlag,
                               label: {
                                EmptyView()
                               })
            }
        }
    }
    
}

struct SecondView: View {
    
    var body: some View {
        Text("Second View")
    }
    
}
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36