TransWikia.com

What is the UIButton Navigation push equivalent in SwiftUI

Stack Overflow Asked by RealTechyGod on September 17, 2020

I have a workspace that I need the main view that use buttons to push to the other projects that contain their respective views. This was simple to achieve in UI Storyboard however it does not seem to be here without implementing a navigationView.

Button(action: {
    print("Button action")
}) {
    Image("LIVEMENU")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)

Is the current implemention as it was a placeholder til now.

This is what I was used to

- (IBAction)GoToNext:(id)sender 
{
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

[[self navigationController] pushViewController:vc2 animated:YES];
}

One Answer

You need to use a NavigationView but you may hide it.

Try the following:

struct ContentView: View {
    @State private var isLinkActive = false

    var body: some View {
        NavigationView {
            VStack {
                Button("Go to...") {
                    // activate link programatically
                    self.isLinkActive = true
                }
            }
            // hide navigation bar
            .navigationBarTitle("", displayMode: .inline)
            .navigationBarHidden(true)
            .background(
                NavigationLink(destination: SecondView(), isActive: $isLinkActive) {
                    EmptyView()
                }
                .hidden()
            )
        }
    }
}

struct SecondView: View {
    @Environment(.presentationMode) private var presentationMode

    var body: some View {
        Button("Go back") {
            self.presentationMode.wrappedValue.dismiss()
        }
        .navigationBarTitle("", displayMode: .inline)
        .navigationBarHidden(true)
    }
}

Correct answer by pawello2222 on September 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP