三、SwiftUI之@ViewBuilder
作者:互联网
什么是@ViewBuilder?
首先,它是一个注解,先看下官方文档的说明:
/// You typically use ``ViewBuilder`` as a parameter attribute for child /// view-producing closure parameters, allowing those closures to provide /// multiple child views. For example, the following `contextMenu` function /// accepts a closure that produces one or more views via the view builder.
翻译为
您通常使用“ViewBuilder”作为子视图生成闭包参数的参数属性, 允许这些闭包提供多个子视图。 例如,下面的 `contextMenu` 函数接受一个通过视图构建器生成一个或多个视图的闭包。
案例
func contextMenu<MenuItems : View>(@ViewBuilder menuItems: () -> MenuItems) -> some View myView.contextMenu { Text("Cut") Text("Copy") Text("Paste") if isSymbol { Text("Jump to Definition") } }
看到这里,想必已经了解ViewBuilder的用途了
标签:闭包,contextMenu,Text,views,ViewBuilder,视图,SwiftUI 来源: https://blog.csdn.net/xinshoubbs/article/details/120777637