swift – Rotate Gadget drawback utilizing NavigationView> Listing > NavigationLink (in iOS 15)

[ad_1]

I’ve a Listing with some rows in NavigationView, every row within the Listing has a NavigationLink to the DetailsView.

The Listing has a unique design in Panorama and Portrait mode. Once I’m rotating machine, design of the Listing is altering, so the whole lot nice.

The issue happens after I first open DetailsView after which rotate machine – DetailsView disappears and design of the Listing is “damaged”.

I’ve this drawback solely in iOS 15 variations, in iOS 16 and up the whole lot is OK.

Right here is the way it appears:

Here is how it looks:

Right here is my code:

import SwiftUI

struct ContentView: View {
    @State non-public var orientation = UIDevice.present.orientation
    
    var physique: some View {
        NavigationView {
            Listing {
                ForEach(1..<21) { index in
                    RowView(index: index, orientation: orientation)
                }
            }
            .navigationTitle("Listing")
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .detectOrientation($orientation)
    }
}



struct RowView: View {
    var index: Int
    var orientation: UIDeviceOrientation
    
    var physique: some View {
        if orientation.isLandscape { // LANDSCAPE
            HStack {
                Picture("myImg").resizable().scaledToFit().body(peak: 80).cornerRadius(6)
                
                NavigationLink(vacation spot: DetailsView(index: index), label: {}).body(width: 0, peak: 0).opacity(0)
                
                VStack {
                    Textual content("Title - (index)").font(.physique).lineLimit(2)
                    Textual content("Descrion").font(.subheadline).foregroundColor(.secondary)
                    Spacer()
                }
            }
        } else { // PORTAIT
            VStack {
                Picture("myImg").resizable().scaledToFit()
                
                NavigationLink(vacation spot: DetailsView(index: index), label: {}).body(width: 0, peak: 0).opacity(0)
                
                VStack {
                    Textual content("Title - (index)").font(.physique).lineLimit(2)
                    Textual content("Descrion").font(.subheadline).foregroundColor(.secondary)
                }
            }
        }
    }
}


struct DetailsView: View {
    var index: Int
    
    var physique: some View {
        Spacer()
        Textual content("Particulars (index)").font(.title)
        Spacer()
    }
}


struct DetectOrientation: ViewModifier {
    @Binding var orientation: UIDeviceOrientation
    
    func physique(content material: Content material) -> some View {
        content material
            .onReceive(NotificationCenter.default.writer(for: UIDevice.orientationDidChangeNotification)) { _ in
                orientation = UIDevice.present.orientation
            }
            .onAppear {
                if let scene = UIApplication.shared.connectedScenes.first,
                   let sceneDelegate = scene as? UIWindowScene,
                   sceneDelegate.interfaceOrientation.isPortrait {
                     orientation = .portrait
                 } else {
                     orientation = .landscapeLeft
                 }
            }
    }
}

extnsion View {
    func detectOrientation(_ orientation: Binding<UIDeviceOrientation>) -> some View {
        modifier(DetectOrientation(orientation: orientation))
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

[ad_2]

Leave a Reply