本文主要是介绍SwiftUI的context Menu,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SwiftUI的 context Menu
现在来演示一下如何使用 SwiftUI 的 Context Menu 。
代码:
import SwiftUIstruct ContextMenuBootCamp: View {@State var bgColor: Color = .purplevar 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()
}
长按组件,效果如下:
这篇关于SwiftUI的context Menu的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!