First, start Xcode -> Create a new project -> Single View App -> Name the app and select the user interface SwiftUI Open the ContentView.swift file.
Delete all the old code, and add the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | struct ContentView: View { @State var index = 0 var body: some View { VStack(spacing: 0){ ZStack{ if self.index == 0{ Color.green } else if self.index == 1{ Color.yellow } else if self.index == 2{ Color.blue } else{ Color.orange } } CircleTab(index: self.$index) } .edgesIgnoringSafeArea(.top) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
In the above code we are creating the tab bar page, currently just screens with color, this tab bar page will be decided according to the variable State index. The CirleTab code snippet is the part where we will render the tab bar.
Continue adding the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | struct CircleTab : View { @Binding var index : Int var body : some View{ HStack{ Button(action: { self.index = 0 }) { VStack{ if self.index != 0{ Image("ranking").resizable().foregroundColor(Color.black.opacity(0.5)) .frame(width: 30, height: 30) } else{ Image("ranking") .resizable() .frame(width: 25, height: 25) .foregroundColor(.white) .padding(5) .background(Color.red) .clipShape(Circle()) //.offset(y: -20) } // Text("BXH").foregroundColor(Color.black.opacity(0.7)).offset( y: 0) } } Spacer() Button(action: { self.index = 1 }) { VStack{ if self.index != 1{ Image("playing").resizable().foregroundColor(Color.black.opacity(0.5)) .frame(width: 30, height: 30) } else{ Image("playing") .resizable() .frame(width: 25, height: 25) .foregroundColor(.white) .padding(5) .background(Color.red) .clipShape(Circle()) //.offset(y: -20) } // Text("Chơi").foregroundColor(Color.black.opacity(0.7)).offset( y: 0) } } Spacer() Button(action: { self.index = 2 }) { VStack{ if self.index != 2{ Image("news").resizable().foregroundColor(Color.black.opacity(0.5)) .frame(width: 30, height: 30) } else{ Image("news") .resizable() .frame(width: 25, height: 25) .foregroundColor(.white) .padding(5) .background(Color.red) .clipShape(Circle()) // .offset(y: -20) } // Text("Tin tức").foregroundColor(Color.black.opacity(0.7)).offset( y: 0) } } Spacer() Button(action: { self.index = 3 }) { VStack{ if self.index != 3{ Image("setting").resizable().foregroundColor(Color.black.opacity(0.5)) .frame(width: 30, height: 30) } else{ Image("setting") .resizable() .frame(width: 25, height: 25) .foregroundColor(.white) .padding(5) .background(Color.red) .clipShape(Circle()) // .offset(y: -20) } //Text("Cài đặt").foregroundColor(Color.black.opacity(0.7)) } } } .cornerRadius(5) .animation(.spring()) .padding(5) .padding(.leading, 10) .padding(.trailing, 10) .padding(.bottom, 2) .background(Color.yellow) } } |
In the code above we will see 4 tab bar button added. And there is a structure of code that is repeated, by checking the index variable. If the index is equal to the tab bar button index it will set the UI for the active button, and vice versa, it is inactive. You also need to add the corresponding image Asset to the tabbar buttons.
After completing the code, run it and you will get the following output