我有一個帶有NavigationView和的水平選單NavigationLink。以下是代碼。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
VStack {
HorizontalView()
Spacer()
}
}
}
}
}
struct MenuModel: Hashable, Identifiable {
let id: Int
let name: String
}
struct HorizontalView: View {
@State var horizonModels = [MenuModel]()
@State var isShowing = false
@State var destIndex: Int = 0
var body: some View {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 30.0) {
ForEach(horizonModels) { item in
NavigationLink(isActive: $isShowing) {
DestView(destIndex: destIndex)
} label: {
Button(action: {
destIndex = item.id
isShowing = true
}, label: {
Text(item.name)
}).foregroundColor(.white)
}
}
}
.padding(.horizontal, 20.0)
.frame(height: 36.0)
.background(Color.red)
}
.navigationBarTitleDisplayMode(.inline)
.navigationViewStyle(StackNavigationViewStyle())
Spacer()
}.onAppear {
horizonModels = [
MenuModel.init(id: 0, name: "Apple"),
MenuModel.init(id: 1, name: "Banana"),
MenuModel.init(id: 2, name: "Peach"),
MenuModel.init(id: 3, name: "Grape"),
MenuModel.init(id: 4, name: "Pineapple"),
MenuModel.init(id: 5, name: "Watermelon"),
MenuModel.init(id: 6, name: "Blueberry")
]
}
}
}
struct DestView: View {
@State var destIndex: Int = 0
var body: some View {
if destIndex == 0 {
// goigng to another view...
}
else {
// goigng to another view...
}
}
}
螢屏看起來像這張照片。除了模擬器顯示以下內容外,一切看起來都很好。
SwiftUI 在推送 aNavigationLink 時遇到問題。請提交錯誤。
這是我需要擔心的事情嗎?我在這里找到一個可能與此問題相關的主題,這對我來說似乎沒什么幫助。
謝謝。
uj5u.com熱心網友回復:
我不知道你為什么會收到警告,但你可以使用這種方法來讓它消失:
NavigationLink(destination: DestView(destIndex: item.id)) {
Text(item.name).foregroundColor(.white)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398788.html
標籤:ios 迅捷 导航视图 swiftui-navigationlink
上一篇:這個標簽欄控制器應該嵌入在哪里?
下一篇:JS&React-forEach和ForLoop只給出一個結果,當我console.log陣列時,它顯示了多個專案avaialbe
