I want to set an image as the background of this TabBar
I tried by many way but it can't
Asked
Active
Viewed 111 times
0

Horitsu Potter
- 107
- 1
- 6
-
1Can you share your code for each tab? – xTwisteDx Jan 30 '22 at 17:00
-
Each Tab is a View, but in that Image, I use Text("") only – Horitsu Potter Jan 30 '22 at 17:13
-
1Use something like a label instead. For example `Label("Something", systemName: "house.fill.")` – xTwisteDx Jan 30 '22 at 20:37
-
I want to change the background of TabBar by an Image, not the TabBar Icon – Horitsu Potter Jan 31 '22 at 07:47
-
I think this will help you https://stackoverflow.com/questions/59215407/swiftui-custom-tabbar-icons – user2058234 Jan 31 '22 at 09:17
-
this post will give you the answer: https://stackoverflow.com/questions/70928399/i-want-to-set-a-background-to-the-tabbar – workingdog support Ukraine Jan 31 '22 at 23:26
2 Answers
1
I have found the default SwiftUI TabBar
to be tricky to work with, but luckily it's not too hard to make a custom TabBar
. This lets you add images or any other View
as the "tabs."
Example Code:
struct ContentView: View {
@State var selectedTab: TabPicker.MenuTab = .first
var body: some View {
VStack {
Text("Hello, World!")
Spacer(minLength: 0)
HStack {
TabPicker(selectedTab: $selectedTab)
}
}
}
}
struct TabPicker: View {
enum MenuTab {
case first
case second
case third
}
@Binding var selectedTab: MenuTab
var body: some View {
HStack {
Spacer() // spacers ensure equal spacing between elements
Text("1") // change these to be whatever views you need
.fontWeight( selectedTab == .first ? .bold: .light) // some way of indicating which tab is selected
.onTapGesture { selectedTab = .first }
Spacer()
Text("2")
.fontWeight( selectedTab == .second ? .bold: .light)
.onTapGesture { selectedTab = .second }
Spacer()
Text("3")
.fontWeight( selectedTab == .third ? .bold: .light)
.onTapGesture { selectedTab = .third }
Spacer()
}
}
}

John Sorensen
- 710
- 6
- 29
0
you have to set it as a @State var like so:
@State var selectedTab: Int = 0
then:
TabView(selection: $selectedTab) {
YourView()
.tabItem{
VStack {
Text("First")
}
}.tag(0)
then create YourView

Roman R
- 39
- 8