摘要:
TabView:
图片+文字组成tabItem,选中时改变图片和文字颜色
跳转到二级页面时隐藏tabbar,返回到首页时显示tabbar
首页、我的两个tab,效果图:
图片文字组成tabItem,选中时改变图片和文字颜色
代码:
struct MainContentView: View {
@State var selection = 0
var body: some View {
TabView(selection: $selection,
content: {
HomeView().tabItem() {
Image(selection == 0 ? "tab_select_home" : "tab_home")
Text("首页")
}.tag(0)
MineView().tabItem {
Image(selection == 1 ? "tab_select_mine" : "tab_mine")
Text("我的")
}.tag(1)
}).accentColor(.red)
}
}
struct MainContentView_Previews: PreviewProvider {
static var previews: some View {
MainContentView()
}
}
TabView选中tab的索引绑定了selection变量,可以改变selection值来手动切换tab,TabView切换tab时也会把值赋给selection
网友评论