效果图
思路
主要使用了TabView,写UI之前要先了解一下swift基本语法,要不然真没法看 不是太懂,看了别的博主的笔记
完整代码
import SwiftUI
struct ContentView: View {
var body: some View {
InitTabView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
ContentView()
}
}
}
struct InitTabView: View {
init() {
let itemAppearance = UITabBarItemAppearance()
itemAppearance.normal.iconColor = UIColor(Color.white)
itemAppearance.selected.iconColor = UIColor(Color.green)
itemAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor(Color.white)]
itemAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor(Color.green)]
itemAppearance.normal.badgeBackgroundColor = UIColor(Color.red)
itemAppearance.selected.badgeBackgroundColor = UIColor(Color.red)
itemAppearance.normal.badgeTextAttributes = [.foregroundColor: UIColor(Color.white)]
itemAppearance.selected.badgeTextAttributes = [.foregroundColor: UIColor(Color.white)]
let appeareance = UITabBarAppearance()
appeareance.stackedLayoutAppearance = itemAppearance
appeareance.backgroundImage = UIImage(named: "test")
appeareance.backgroundColor = UIColor(Color.gray)
appeareance.shadowColor = UIColor(Color.gray)
appeareance.stackedItemPositioning = .centered
appeareance.stackedItemSpacing = 2000
UITabBar.appearance().scrollEdgeAppearance = appeareance
}
@State private var varSelected = 1
var body: some View {
TabView(selection: $varSelected) {
Text("Home Tab")
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
Text("DisCover Tab")
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "safari")
Text("DisCover")
}
.badge(10)
Text("Profile Tab")
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "person.crop.circle")
Text("Profile")
}
}
.accentColor(Color.green)
}
}
|