简体   繁体   中英

iOS Swift SwiftUI - Use NavigationLink after clicking on subtext

I got this text:

Text("Indem du fortfährst, stimmst du unseren  ") +
Text("Nutzungsbedingungen")
    .underline()
    .foregroundColor(Color("ClickableLink")) +
Text(" und unserer ") +
Text("Datenschutzerklärung")
    .underline()
    .foregroundColor(Color("ClickableLink")) +
Text(" zu")

I'd like to open a new view using NavigationLink after taping on Nutzungsbedingungen or Datenschutzerklärung , both need to open different views.

I've seen those answers:

SwiftUI tappable subtext

but those are either not what I need or trying them gives me errors and I don't know how to modify them since I'm absolutely new to swift/swiftui

Here are two possible approaches, one using the new NavigationStack with NavigationDestination and the other using popOver .

Both have the downside that you can't concatenate Text views within them. But you can still work with HStack/VStack to define the layout.

enum Legals: Identifiable {
    case nutzungsbedingungen
    case datenschutzerklärung
    
    var id: Legals { self }
}

struct ContentView: View {
        
    @State private var selected: Legals?
    
    var body: some View {
        NavigationStack {
            
            // NavigationLink version
            VStack(alignment: .leading) {
                Text("Indem du fortfährst, stimmst du unseren")
                NavigationLink("Nutzungsbedingungen", value: Legals.nutzungsbedingungen)
                Text("und unserer")
                NavigationLink("Datenschutzerklärung", value: Legals.datenschutzerklärung)
                Text("zu")
            }
            .navigationDestination(for: Legals.self) { selection in
                switch selection {
                case .datenschutzerklärung:
                    Text("Datenschutzerklärung")
                case .nutzungsbedingungen:
                    Text("Nutzungsbedingungen")
                }
            }
            
            Divider()
            
            // popOver Version
            VStack(alignment: .leading) {
                Text("Indem du fortfährst, stimmst du unseren ")
                Text("Nutzungsbedingungen")
                    .underline()
                    .onTapGesture {
                        selected = .nutzungsbedingungen
                    }
                Text("und unserer ")
                Text("Datenschutzerklärung")
                    .underline()
                    .onTapGesture {
                        selected = .nutzungsbedingungen
                    }
                Text("zu")
            }
            .popover(item: $selected) { selected in
                switch selected {
                case .datenschutzerklärung:
                    Text("Datenschutzerklärung")
                case .nutzungsbedingungen:
                    Text("Nutzungsbedingungen")
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM