SwiftUI的 context Menu
现在来演示一下如何使用 SwiftUI 的 Context Menu 。
代码:
import SwiftUI
struct ContextMenuBootCamp: View {
@State var bgColor: Color = .purple
var body: some View {
VStack(alignment: .leading, spacing: 10.0) {
Image(systemName: "house.fill")
.font(.title)
Text("Swiftful Thinking")
.font(.headline)
Text("how to use context menu".localizedCapitalized)
.font(.subheadline)
}
.foregroundColor(.white)
.padding(30)
.background(bgColor)
.cornerRadius(30)
.contextMenu(ContextMenu(menuItems: {
Button(action: {
bgColor = .red
}, label: {
Label("Button #1", systemImage: "flame.fill")
})
Button(action: {
bgColor = .orange
}, label: {
Label("Button #2", systemImage: "flame.fill")
})
Button(action: {
bgColor = .green
}, label: {
Label("Button #3", systemImage: "flame.fill")
})
}))
}
}
#Preview {
ContextMenuBootCamp()
}
长按组件,效果如下: