NavigationStack SwiftUI 4.0 的全新导航系统
使用 SwiftUI 的 NavigationStack 组件进行页面导航
改变颜色 SwiftUI navigation bar 的背景顏色
SwiftUI changing navigation bar background color for inline navigationBarTitleDisplayMode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 struct ContentView : View { var body: some View { NavigationStack { List { Text ("Item 1" ) Text ("Item 2" ) } .navigationTitle("My List" ) .toolbarBackground( Color .blue, for: .navigationBar ) .toolbarBackgroundVisibility(.visible, for: .navigationBar) } } }
跳转的几种方式 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 { NavigationStack (path: $path ) { List { NavigationLink ("跳转方式一" ) { } NavigationLink ("跳转方式一" , destination: { }) NavigationLink (destination: { }, label: { LabeledContent ("跳转方式" , value: "一" ) }) Button (action: { isPresented.toggle() }, label: { Text ("跳转方式二" ) }) NavigationLink ("跳转方式三" , value: "3&4" ) Button (action: { path.append("3&4" ) }, label: { Text ("跳转方式四" ) }) ForEach (items) { item in NavigationLink (value: item) { Text (item.text) } } ForEach (items) { item in Text (item.text) .onTapGesture { path.append(item) } } } } .navigationDestination(isPresented: $isPresented ) { } .navigationDestination(for: String .self ) { destination in if destination == "3&4" { } } .navigationDestination(for: Item .self ) { item in } } }struct Item : Identifiable , Hashable { let id = UUID () let text: String }
隐藏 NavigationLink 的箭头 常规手段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 List { Text ("The cell" ) .background( NavigationLink (value: item) { EmptyView () }.opacity(0 ) ) }List { ZStack { NavigationLink (value: item) { EmptyView () }.opacity(0 ) Text ("The cell" ) } }
非常规手段,使用 .navigationLinkIndicatorVisibility(.hidden),参考:
隱藏 & 顯示向右箭頭的 navigationLinkIndicatorVisibility
navigationLinkIndicatorVisibility(_:)
但不知道为什么,反正我用不了。
NavigationSplitView
NavigationStack inside NavigationSplitView detail in SwiftUI
隐藏 NavigationSplitView 的 detail 1 2 3 4 5 6 7 8 9 10 11 12 NavigationSplitView { Text ("Sidebar" ) } content: { Text ("Content" ) } detail: { if detailViewVisible { Text ("Detail" ) } else { Spacer () .navigationSplitViewColumnWidth(0 ) } }