使用交互式用户界面元素在多个子视图之间切换的视图。
struct TabView<SelectionValue, Content> where SelectionValue : Hashable, Content : View
要创建带有选项卡的用户界面,请在Tab
中放置视图,并将tabItem(_:)
修饰符应用于每个选项卡的内容。在iOS上,您还可以使用徽章修饰符之一,如badge(_:)
为每个选项卡分配徽章。
以下示例创建一个带有三个选项卡的选项卡视图,每个选项卡都显示自定义子视图。第一个选项卡有一个数字徽章,第三个选项卡有一个字符串徽章。
TabView {
ReceivedView()
.badge(2)
.tabItem {
Label("Received", systemImage: "tray.and.arrow.down.fill")
}
SentView()
.tabItem {
Label("Sent", systemImage: "tray.and.arrow.up.fill")
}
AccountView()
.badge("!")
.tabItem {
Label("Account", systemImage: "person.crop.circle.fill")
}
}
为每个标签项目使用
Label
,或者可选地使用Text
、Image
或后跟文本的图像。传递任何其他类型的视图都会产生可见但空的选项卡项。
创建标签页视图
init(content: () -> Content)
当Selection为Int且Content符合View时可用。
init(selection: Binding<SelectionValue>?, content: () -> Content)
创建一个从与Selection值关联的内容中选择的实例。
支持类型
var body: some View
视图的内容和行为。
typealias Body
代表此视图主体的视图类型。
func tabViewStyle<S>(S) -> some View
在当前环境中设置选项卡视图的样式。
func tabItem<V>(() -> V) -> some View
设置与此视图关联的选项卡栏项。