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

相关推荐
东坡肘子4 天前
SPI 加入 Apple,Swift 迈向自举 -- 肘子的 Swift 周报 #142
人工智能·swiftui·swift
神奇的程序员9 天前
开发了一个进阶版Apple健康
swiftui·apple·apple watch
东坡肘子11 天前
Swift 还让你 Excited 吗?-- 肘子的 Swift 周报 #141
人工智能·swiftui·swift
sweet丶13 天前
Swift 元编程-Macro
swift
影寂ldy17 天前
WinForm PictureBox控件 + ImageList组件 完整笔记
开发语言·笔记·swift
Deepzz17 天前
macOS 上调教第三方鼠标的一些经验:从滚动顺滑到输入法自动切换
macos·swift·鼠标
大熊猫侯佩17 天前
WWDC26:SwiftUI 8 的 @State 全新“懒加载”机制与最佳实践
性能优化·swiftui·observable·懒加载·state·swift宏·实例初始化
东坡肘子18 天前
WWDC 26:AI 帮你看完了,然后呢?-- 肘子的 Swift 周报 #140
人工智能·swiftui·swift
大熊猫侯佩19 天前
SwiftData 迁移深度指南:从入门到“填坑”(下集)
数据库·swift·编程语言
大熊猫侯佩19 天前
SwiftData 迁移深度指南:从入门到“填坑”(上集)
数据库·swift·编程语言