SwiftUI NavigationStack&NavigationSplitView 合集

SwiftUI 4.0 的全新导航系统

使用 SwiftUI 的 NavigationStack 组件进行页面导航

自定义你的 NavigationStack 导航栏外观

跳转的几种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
struct NavigationLinkView: View {
@State private var path: NavigationPath = .init()
@State private var isPresented: Bool = false
@State private var items: [Item] = [Item(text: "ForEach 中跳转")]
var body: some View {
// 使用 NavigationLink 会导致组件多一个箭头
NavigationStack(path: $path) {
List {
NavigationLink("跳转方式一") {
// destination
}
NavigationLink("跳转方式一", destination: {
// destination
})
NavigationLink(destination: {
// destination
}, label: {
LabeledContent("跳转方式", value: "一")
})

Button(action: {
isPresented.toggle()
}, label: {
Text("跳转方式二")
})

NavigationLink("跳转方式三", value: "3&4")

Button(action: {
path.append("3&4")
}, label: {
Text("跳转方式四")
})

// ForEach 跳转方式一
ForEach(items) { item in
NavigationLink(value: item) {
Text(item.text)
}
}

// ForEach 跳转方式二
ForEach(items) { item in
Text(item.text)
.onTapGesture {
path.append(item)
}
}

}
}
// 跳转方式二
.navigationDestination(isPresented: $isPresented) {
// destination
}

// 跳转方式三 & 四
.navigationDestination(for: String.self) { destination in
if destination == "3&4" {
// destination
}
}

// ForEach 跳转
.navigationDestination(for: Item.self) { item in
// destination
}
}
}

struct Item: Identifiable, Hashable {
let id = UUID()
let text: String
}

常规手段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List {
Text("The cell")
.background( // 使用 overlay 也可以
NavigationLink(value: item) {
EmptyView()
}.opacity(0)
)
}

// 或者

List {
ZStack {
NavigationLink(value: item) {
EmptyView()
}.opacity(0)
Text("The cell")
}
}

非常规手段,使用 .navigationLinkIndicatorVisibility(.hidden),参考:

隱藏 & 顯示向右箭頭的 navigationLinkIndicatorVisibility

navigationLinkIndicatorVisibility(_:)

但不知道为什么,反正我用不了。

  1. NavigationStack inside NavigationSplitView detail in SwiftUI

SwiftUI NavigationStack&NavigationSplitView 合集
https://wonderhoi.com/2024/12/09/SwiftUI-NavigationStack-NavigationSplitView-合集/
作者
wonderhoi
发布于
2024年12月9日
许可协议