Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to a new view using SwiftUI

Tags:

swift

swiftui

I've got a basic view with a button using SwiftUI and I'm trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegate for this view that will tell the app's SceneDelegate to present a new view controller?

import SwiftUI  struct ContentView : View {     var body: some View {         VStack {             Text("Hello World")             Button(action: {                 //go to another view             }) {                 Text("Do Something")                     .font(.largeTitle)                     .fontWeight(.ultraLight)             }         }     } } 
like image 494
Jake Avatar asked Jun 04 '19 04:06

Jake


People also ask

How do I navigate to a different view in SwiftUI?

Getting Started with Navigation Links:Text("Navigate here.") Text("Now on the second view.") As you can see from the example above, the navigation link will act as a standard link. Once clicked, the application will then navigate to and display the view that was set as the destination.

How do I push new view in SwiftUI?

Allowing to push a new screen onto a navigation stack is as simple as wrapping your SwiftUI views into a NavigationLink wrapper. As long as you contain your views in a navigation view, you'll be able to push new destination views.

How do I use present view in SwiftUI?

To use a sheet, give it something to show (some text, an image, a custom view, etc), add a Boolean that defines whether the detail view should be showing, then attach it to your main view as a modal sheet. Important: If you're targeting iOS 14 or below, you should use @Environment(\.


2 Answers

This was not intended to be the top answer here - but rather an alternative method if you didn't want the navigation bar. See Jake's answer below for the normal way to do this with NavigationView & NavigationLink. Hopefully this is still useful or points you in the right direction too.

If you are just trying to get a NavigationLink working with a Binding, you can use the same NavigationLink init I did with the isActive binding parameter.

Anyway, back to the answer…


I made a view modifier for this. It also means that there is no navigation bar. You can call it like so:

.navigate(to: MainPageView(), when: $willMoveToNextScreen) 

This can be attached to anything, so I typically attach it to the end of the body, for example:

@State private var willMoveToNextScreen = false  var body: some View {     VStack {         /* ... */     }     .navigate(to: MainPageView(), when: $willMoveToNextScreen) } 

Code (remember to import SwiftUI):

extension View {     /// Navigate to a new view.     /// - Parameters:     ///   - view: View to navigate to.     ///   - binding: Only navigates when this condition is `true`.     func navigate<NewView: View>(to view: NewView, when binding: Binding<Bool>) -> some View {         NavigationView {             ZStack {                 self                     .navigationBarTitle("")                     .navigationBarHidden(true)                  NavigationLink(                     destination: view                         .navigationBarTitle("")                         .navigationBarHidden(true),                     isActive: binding                 ) {                     EmptyView()                 }             }         }         .navigationViewStyle(.stack)     } } 
like image 75
George Avatar answered Sep 21 '22 13:09

George


The key is to use a NavigationView and a NavigationLink:

import SwiftUI  struct ContentView : View {     var body: some View {         NavigationView {             VStack {                 Text("Hello World")                 NavigationLink(destination: DetailView()) {                     Text("Do Something")                 }             }         }     } } 
like image 23
Jake Avatar answered Sep 24 '22 13:09

Jake