SwiftUI Swift 多个 sheet

今天做一个多个 sheet 的效果,点击下面三个按钮打开不同的 sheet


Show me the code

swift 复制代码
import SwiftUI

enum CurrentActiveSheet: Identifiable {
    case add, edit, delete

    var id: Int {
        hashValue
    }
}

struct MoreSheet: View {
    @State var currentActiveSheet: CurrentActiveSheet?

    var body: some View {
        HStack(spacing: 20) {
            Text("add")
                .padding()
                .background(Color.red)
                .onTapGesture {
                    currentActiveSheet = .add
                }
            
            Text("edit")
                .padding()
                .background(Color.green)
                .onTapGesture {
                    currentActiveSheet = .edit
                }
            
            Text("delete")
                .padding()
                .background(Color.blue)
                .onTapGesture {
                    currentActiveSheet = .delete
                }
        }
        .foregroundColor(Color.white)
        .sheet(item: $currentActiveSheet) { item in
            switch item {
            case .add:
                Text("add")
            case .edit:
                Text("edit")
            case .delete:
                Text("delete")
            }
        }
    }
}

struct MoreSheet_Previews: PreviewProvider {
    static var previews: some View {
        MoreSheet()
    }
}

Preview


喜欢或对你有帮助,点个赞吧,自己先点个嘿嘿。
有错误或者疑问还请评论指出。
我的个人网站 点击访问 hongweizhu.com

END

相关推荐
songgeb2 天前
Concurrency in Swift学习笔记-初识
ios·swift
杂雾无尘2 天前
2025 年了,是否该全面拥抱 Swift 6?
ios·swift·客户端
杂雾无尘3 天前
用高斯公式优化 Swift 代码,让运行速度飞跃数十万倍!
ios·swift·apple
杂雾无尘4 天前
解决 Xcode 烦人错误:"Build input file cannot be found" 一招搞定!
ios·swift·客户端
水木姚姚5 天前
图书管理软件iOS(iPhone)
macos·ios·iphone·xcode·swift
东坡肘子5 天前
F1:电影很好看,赛事很挣钱 | 肘子的 Swift 周报 #094
swiftui·swift·apple
YungFan6 天前
iOS26适配指南之动画
ios·swift
瓜子三百克6 天前
Swift6.1 - 基础知识1: 简单值、控制流、函数和闭包
开发语言·swift
songgeb7 天前
Currying and Partial application
swift·函数式编程
Keya9 天前
lipo 命令行指南
ios·xcode·swift