0

Mockup of the Application How to navigate to another view Problem:

My application successfully navigates from one view to another without any complexities.When I use the navigationLink to navigate from View 4 to View 2 (refer mockup). The view 2 movesup. I tried debugging but I found no solution.

I have designed a mockup of what I am trying to acheive.

Code Block for View 4:

import SwiftUI
import BLE

struct View4: View {

    @EnvironmentObject var BLE: BLE
    @State private var showUnpairAlert: Bool = false
    @State private var hasConnected: Bool = false
    @State private var activateLink: Bool = false

    let defaults = UserDefaults.standard
    let defaultDeviceinformation = "01FFFFFFFFFF"

    struct Keys {
        static let deviceInformation = "deviceInformation"
    }

    var body: some View {
        VStack(alignment: .center, spacing: 0) {
          NavigationLink(destination: View2(), isActive: $activateLink,label: { EmptyView() })
            // MARK: - Menu Bar
            HStack(alignment: .center, spacing: 10) {
                VStack(alignment: .center, spacing: 4) {
                    Text(self.hasConnected ? "PodId \(checkForDeviceInformation())":"Pod is not connected")
                        .font(.footnote)
                        .foregroundColor(.white)
                    Button(action: {
                        print("Unpair tapped!")
                        self.showUnpairAlert = true
                    }) {
                        HStack {
                            Text("Unpair")
                                .fontWeight(.bold)
                                .font(.body)
                        }
                        .frame(minWidth: 85, minHeight: 35)
                        .foregroundColor(.white)
                        .background(Color(red: 0.8784313725490196, green: 0.34509803921568627, blue: 0.36470588235294116))
                        .cornerRadius(30)
                    }
                }
            }
        }
        .alert(isPresented: $showUnpairAlert) {
            Alert(title: Text("Unpair from \(checkForDeviceInformation())"), message: Text("Do you want to unpair the current pod?"), primaryButton: .destructive(Text("Unpair")) {
                self.unpairAndSetDefaultDeviceInformation()
                }, secondaryButton: .cancel())
        }
    }

    func checkForDeviceInformation() -> String {
        let deviceInformation = defaults.value(forKey: Keys.deviceInformation) as? String ?? ""
        print("Device Info \(deviceInformation)")
        return deviceInformation
    }

    func unpairAndSetDefaultDeviceInformation() {
        defaults.set(defaultDeviceinformation, forKey: Keys.deviceInformation)
        print("Pod unpaired and view changed to Onboarding")
        DispatchQueue.main.async {
        self.activateLink = true
       }
    }

}

Thank you !!!!

Denzil
  • 415
  • 5
  • 13
  • 1
    If you mean unwind from view4 directly to view2 bypassing view3, then such feature is not supported by SwiftUI `NavigationView` yet. – Asperi Dec 12 '19 at 18:35
  • Here is one reference https://stackoverflow.com/questions/59200936/swiftui-navigate-to-bottom-of-navigationview-stack/59204024#59204024 – E.Coms Dec 12 '19 at 18:35
  • @Asperi is there a way to achieve this in any way? – Denzil Dec 12 '19 at 20:13
  • probably you could find [How do I add Animations to Transitons between custom NavigationItems made from AnyView?](https://stackoverflow.com/questions/58908741/how-do-i-add-animations-to-transitons-between-custom-navigationitems-made-from-a/59087574#59087574) topic helpful – Asperi Dec 13 '19 at 05:58

0 Answers0