UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI Navigation View - Handling Size Classes over multiple views

Forums > SwiftUI

@ecjps  

I have the scenario where I intend on using a menu ('MenuView') that varies if the device is in portait / landscape mode. I am using size classes to determine the device type and this succesfully redraws the view on rotation. The menu uses navigation view / links to take you to a further view (the 'DetailedView'); this view also has differing views for portrait and landscape. Again I'm using size classes to successfully redraw the view based on the rotation.

However, what I find is that when I'm in the DetailedView and rotate the device, the display jumps straight back to the MenuView as, of course, it has recognised the size class change and adjusted that view. I would like the display to remain on this view.

How can I prevent the app from jumping to the 'MenuView' when I rotate the device that is displaying the 'DetailedView'? Code from the MenuView below:

Note: I'm using the StackNavigationViewStyle in this instance.

Any help would be gratefully received, thanks in advance!

struct MenuView: View {

    @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
    @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?

    var body: some View {

        NavigationView {

            Color("MainBg")
                .edgesIgnoringSafeArea(.all)

            if horizontalSizeClass == .compact {
                PortraitMenuView()
            } else {
                LandscapeMenuView()
            }

        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.