在swiftui中实现导航链接时,视图向上移动

时间:2019-12-12 18:25:48

标签: swift swiftui swift5 swift5.1 swiftui-navigationlink

应用程序模型 How to navigate to another view 问题:

我的应用程序成功地从一个视图导航到另一个视图而没有任何复杂性。当我使用navigationLink从View 4导航到View 2时(请参见模型)。视图2向上移动。我尝试调试,但没有找到解决方案。

我设计了一个我想要达到的目标的模型。

视图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
       }
    }

}

谢谢!!!!

0 个答案:

没有答案