Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How to change rootview controller programmatically

Tags:

ios

swift

swiftui

I am trying to change the rootview controller to a different screen after the user has logged out.

    if let window = UIApplication.shared.windows.first {
        window.rootViewController = UIHostingController(rootView: SignInView())
        window.endEditing(true)

        window.makeKeyAndVisible()
    }

This is what I have so far. It navigates to the different view but it makes the buttons on the new new view unusable. Is there a better way to change the rootview or am I missing something?

like image 695
lazerrouge Avatar asked Oct 26 '25 10:10

lazerrouge


1 Answers

My approach is to create an observable object that coordinates navigation between the various app views. Below a simplified example to give you an idea:

//Your app views
enum AppViews {
    case LoginView
    case MainAppView
}

//Class that conforms to the ObservableObject protocol and publishes the viewId property.
//Basically your navigation will react to viewId changes

class ShowingView: ObservableObject {
    
    init(showingView: AppViews) {
        self.viewId = showingView
    }
    
    @Published var viewId : AppViews
}

//The current root view of your app is observing changes of the ShowingView class and switch between your app views
struct AppRootView: View {
    
    @ObservedObject var showingView: ShowingView
    
    var body: some View {
        Group {
            if showingView.viewId == .LoginView {
                TermsAndConditionsView()
                    .environmentObject(showingView)
            }
            else {
                MainAppView()
                    .environmentObject(showingView)
            }
        }
    }
}

Notice that you can pass showingView as environmentObject to your views and use it to switch to another view when required. In your case switch to another screen when the user logs out.

In your scene delegate you can then for example switch between login and main view as follows

var appStartView: AppViews

let isloggedIn = UserDefaults.standard.bool(forKey: "IsLoggedIn")

if isLoggedIn == false {
    appStartView = .LoginView
}
else {
    appStartView = .MainAppView
}

let contentView = AppRootView(showingView: ShowingView(showingView: appStartView))

if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(rootView: contentView)
    self.window = window
    window.makeKeyAndVisible()
}

To switch between MainAppView and LoginView you can simply modify the value of the environmentObject showingView

self.showingView.viewId = AppViews.LoginView

I hope it helps to guide you in the right direction

like image 95
SwissMark Avatar answered Oct 28 '25 23:10

SwissMark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!