【iOS】SwiftUI 路由管理(NavigationStack)

QDRouter.swift

swift 复制代码
import SwiftUI

@MainActor
class QDRouter: ObservableObject {
    @Published var path = NavigationPath()

    static let main = QDRouter() // 单例

    private init() {}

    func open(_ url: String) {
        guard let url = URL(string: url) else {
            return
        }
        UIApplication.shared.open(url)
    }

    func push(page: RoutePage, param: [String: String]? = nil) {
        path.append(QDRoute(page: page, param: param))
    }

    func pop() {
        path.removeLast()
    }

    func popToRoot() {
        path.removeLast(path.count)
    }
}

// MARK: route page

enum RoutePage {
    case none
    case center
    case web
}

struct QDRoute: Hashable {
    var page: RoutePage
    var param: [String: String]?
}

// MARK: present view

enum PresentPage {
    case none
    case web
}

class PresentObject: ObservableObject {
    @Published var pageName: PresentPage = .none
    @Published var isPresent: Bool = false
    @Published var param: [String: String]?

    func presentView(pageName: PresentPage, param: [String: String]? = nil, isPresent: Bool = true) {
        self.pageName = pageName
        self.param = param
        self.isPresent = isPresent
    }
}

extension View {
    func withNavDestination() -> some View {
        return navigationDestination(for: QDRoute.self) { route in
            let param = route.param
            switch route.page {
            case .center:
                CenterView(param: param)
            case .none:
                Text("")
            case .web:
                WebView()
            }
        }
    }

    func withPresentDestination(isPresent: Binding<Bool>, pageName: PresentPage, param: [String: String]?) -> some View {
        return fullScreenCover(isPresented: isPresent, onDismiss: {
            print("")
        }, content: {
            switch pageName {
            case .web:
                WebView(param: param)
            case .none:
                Text("")
            }
        })
    }
}

初始页面:

LaunchView.swift

swift 复制代码
import SwiftUI

/// 启动视图
struct LaunchView: View {
    @State private var logoOpacity: Double = 0.0

    @State var isPreview = false

    @StateObject var router = QDRouter.main

    @State var showCenter = false

    @StateObject private var presentObject = PresentObject()

    var body: some View {
        NavigationStack(path: $router.path) {
            VStack {
                if showCenter {
                    CenterView()
                } else {
                    ZStack(alignment: Alignment.bottom, content: {
                        ZStack {
                            Image("startImageNew")
                                .resizable()
                                .edgesIgnoringSafeArea(.all)
                            Image("launchTopLogo").opacity(logoOpacity)
                        }
                        Image("launchBottomLogo")
                    })
                    .onAppear {
                        let duration = 0.5
                        withAnimation(.easeIn(duration: duration)) {
                            logoOpacity = 1.0
                        }
                        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
                            print("动画完成")
                            if !isPreview {
                                self.showCenter = true
                            }
                        }
                    }
                }
            }.withNavDestination()

        }.environmentObject(presentObject)
    }
}

struct LaunchView_Previews: PreviewProvider {
    static var previews: some View {
        LaunchView(isPreview: true)
    }
}

由 LaunchView跳转到CenterView

CenterView.swift

swift 复制代码
import SwiftUI

struct CenterView: View {
    @State private var selectedTab = 0
    @EnvironmentObject var presentObject: PresentObject

    var param: [String: String]?
    var body: some View {
        TabView(selection: $selectedTab) {
            FirstView()
                .tabItem {
                    (selectedTab == 0) ? Image(systemName: "house.fill") : Image(systemName: "house")
                    Text("首页")
                }.tag(0)

            SecondView()
                .tabItem {
                    Image(systemName: "dollarsign.circle")
                    Text("财富")
                }.tag(1)

            ThirdView()
                .tabItem {
                    Image(systemName: "wallet.pass")
                    Text("钱包")
                }.tag(2)

            FourthView()
                .tabItem {
                    Image(systemName: "person")
                    Text("个人")
                }.tag(3)
        }
        .navigationBarBackButtonHidden(true)
        .onAppear {}
        .withPresentDestination(isPresent: $presentObject.isPresent, pageName: presentObject.pageName, param: presentObject.param)
    }
}

#Preview {
    CenterView(param: [:])
}

withNavDestination 用于控制路由的push和pop

withPresentDestination 用于控制present view

具体使用:

swift 复制代码
@EnvironmentObject var presentObject: PresentObject
    var body: some View {
        VStack {
            Text("Hello, World 4")
            Button("present view") {
                presentObject.presentView(pageName: .web)
            }

            Button("push view") {
                QDRouter.main.push(page:.web)
            }
        }
    }
相关推荐
2501_9159214319 分钟前
iOS 应用代上架流程,多工具组合与使用 开心上架 跨平台自动化上传指南
android·ios·小程序·uni-app·自动化·cocoa·iphone
日日行不惧千万里24 分钟前
2025最新仿默往 IM 即时通讯系统源码(PC + Web + iOS + Android)完整版发布!
android·ios
歪歪10024 分钟前
React Native开发Android&IOS流程完整指南
android·开发语言·前端·react native·ios·前端框架
阿里超级工程师2 小时前
ios云打包证书申请不需要苹果电脑也是可以的
ios·证书·云打包
2501_915918414 小时前
iOS 混淆与 IPA 加固一页式行动手册(多工具组合实战 源码成品运维闭环)
android·运维·ios·小程序·uni-app·iphone·webview
东坡肘子4 小时前
去 Apple Store 修手机 | 肘子的 Swift 周报 #0107
swiftui·swift·apple
RollingPin7 小时前
iOS八股文之 网络
网络·网络协议·ios·https·udp·tcp·ios面试
彩旗工作室18 小时前
将iOS/macOS应用上架至App Store
macos·ios·应用商店·appstore
江东小bug王21 小时前
深入解析 iOS 与 macOS 应用程序生命周期(完整指南)
macos·ios
2501_916008891 天前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone