Does anyone know if it's possible or how to change the "More" label to another word in TabView (SwiftUI)?
When there are more than 5 elements or views, the "More" label with three points (ellipsis) is added. I would like to be able to change that word that comes by default and put another word.
var body: some View {
TabView(selection: handler) {
InicioView()
.tabItem {
Label("Inicio", systemImage: "house.circle")
.font(.system(size: 32))
.foregroundColor(Color("verde"))
}.tag(0)
TarjetaRodelagView()
.tabItem {
Label("Tarjeta Rodelag", systemImage: "creditcard.circle")
.font(.system(size: 32))
.foregroundColor(Color("verde"))
}.tag(2)
CategoriasView()
.tabItem {
Label("CategorÃas", systemImage: "list.bullet.rectangle.fill")
.font(.system(size: 32))
.foregroundColor(Color("verde"))
}.tag(1)
InicioView()
.tabItem {
VStack {
Image("ari").resizable().frame(width: 55, height: 55).scaledToFit()
Text("ARI")
}
}.tag(4)
SucursalesView()
.tabItem {
Label("Sucursales", systemImage: "map")
.font(.system(size: 32))
.foregroundColor(Color("verde"))
}.tag(3)
CatalogoView()
.tabItem {
Label("Catálogo", systemImage: "book.circle")
.font(.system(size: 32))
.foregroundColor(Color("verde"))
}.tag(6)
}.accentColor(Color("verde"))
Related
I have a bottom bar with buttons in it. I'm having trouble adding badges to the buttons and tried using the native .badges modifier but had no effect.
This is what I'm trying:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Test")
}
.padding()
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
ControlGroup {
Button(action: {}) {
Label("Button 1", systemImage: "doc")
.badge(1)
}
Button(action: {}) {
Label("Button 2", systemImage: "checkmark")
}
.badge(2)
Button(action: {}) {
Label("Button 3", systemImage: "person")
}
}
}
}
}
}
}
This is what it looks like:
Is there a way to achieve this in SwiftUI?
Documentation of badge modifier states "Badges are only displayed in list rows and iOS tab bars" . Toolbar is not a Tabbar.
A possible approach is to do that in custom way (because toolbar accepts just a view), so it could be like
Tested with Xcode 13.4 / iOS 15.5
ControlGroup {
Button(action: {}) {
Label("Button 1", systemImage: "doc")
}
Button(action: {}) {
Label("Button 2", systemImage: "checkmark")
}
Button(action: {}) {
Label("Button 3", systemImage: "person")
}
}
.overlay(HStack(alignment: .top) { // << here !!
Image(systemName: "1").foregroundColor(.green)
.frame(maxWidth: .infinity)
Image(systemName: "3").foregroundColor(.orange)
.frame(maxWidth: .infinity)
Image(systemName: "9").foregroundColor(.purple)
.frame(maxWidth: .infinity)
}
.frame(maxHeight: .infinity)
.symbolVariant(.fill)
.symbolVariant(.circle)
.allowsHitTesting(false)
.offset(x: 10, y: -10)
// ^^^^ just for demo simplicity, can be somehow dynamic
)
Test code on GitHub
When using TabView in SwiftUI, what can I do to show the selected Tab like in the following picture?
I've tried creating a VStack within each tab like this:
struct ContentView: View {
#State public var tabViewSelection = 0
var body: some View {
TabView(selection: $tabViewSelection) {
HomeFirstLevel()
.tabItem {
VStack {
Image("HomeIcon")
Rectangle()
.frame(height: 7)
.foregroundColor((tabViewSelection == 0) ? .black : .clear)
}
}.tag(0)
}
}
}
But it's not working.
I can't even seem to add a Rectangle instead of an Image:
HomeFirstLevel()
.tabItem {
Rectangle()
}.tag(0)
Does TabView not accept shapes?
Thanks in advance for the help!
You can not set shape in tabItem. But you can use ZStack to add shape over the tab bar and set the x position.
Here is the demo.
struct ContentViewTabDemo: View {
#State public var tabViewSelection = 0
private var singleTabWidth = UIScreen.main.bounds.width / 5
var body: some View {
ZStack(alignment: .bottomLeading) {
TabView(selection: $tabViewSelection) {
Color.red
.tabItem {
VStack {
Image(systemName: "circle.fill")
}
}.tag(0)
Color.blue
.tabItem {
VStack {
Image(systemName: "heart.fill")
}
}.tag(1)
Color.red
.tabItem {
VStack {
Image(systemName: "circle.fill")
}
}.tag(2)
Color.blue
.tabItem {
VStack {
Image(systemName: "heart.fill")
}
}.tag(3)
Color.red
.tabItem {
VStack {
Image(systemName: "circle.fill")
}
}.tag(4)
}
Rectangle()
.offset(x: singleTabWidth * CGFloat(tabViewSelection))
.frame(width: singleTabWidth, height: 7)
.padding(.bottom, 2)
.animation(.default)
}
}
}
I'm trying to add a view to the bottom of a List while remaining in the safe zone. This is what I want to end up with, I'm referring to the "Updated: 5:26 AM":
What I tried is using the Section footer with a spacer, but that didn't work:
struct ContentView: View {
#State private var selectedTab = 0
var body: some View {
TabView(selection: $selectedTab) {
List {
Section {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
Section(
footer: VStack {
Spacer()
Text("Updated at: 5:26 AM")
.frame(maxWidth: .infinity)
}
) {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
}
.listStyle(InsetGroupedListStyle())
.tabItem {
Label("First", systemImage: "alarm")
}
Text("Content 2")
.tabItem {
Label("Second", systemImage: "calendar")
}
}
}
}
Then I tried a VStack as a section of of its own with a Spacer but no luck either:
struct ContentView: View {
#State private var selectedTab = 0
var body: some View {
TabView(selection: $selectedTab) {
List {
Section {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
Section {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
VStack {
Spacer()
Text("Updated at: 5:26 AM")
.font(.footnote)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity)
}
.listRowBackground(Color(.systemGroupedBackground))
}
.listStyle(InsetGroupedListStyle())
.tabItem {
Label("First", systemImage: "alarm")
}
Text("Content 2")
.tabItem {
Label("Second", systemImage: "calendar")
}
}
}
}
How do I achieve this while also considering the List might scroll? I'm trying to tack it on the end of the list, not floating.
You can put both List and Text together in VStack with no spacing:
struct ContentView: View {
#State private var selectedTab = 0
var body: some View {
TabView(selection: $selectedTab) {
VStack(spacing: 0) {
List {
Section {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
Section {
Text("Item 1")
Text("Item 1")
Text("Item 1")
}
}
.listStyle(InsetGroupedListStyle())
Text("Updated at: 5:26 AM")
.font(.footnote)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity)
.background(Color(.systemGroupedBackground))
}
.tabItem {
Label("First", systemImage: "alarm")
}
Text("Content 2")
.tabItem {
Label("Second", systemImage: "calendar")
}
}
}
}
I want to change tabItem icon image when tapped on one of the tabItem in this code. How can it be done?
var body: some View {
ZStack {
TabView(selection:$selection) {
OffersView()
.tabItem ({
Image(systemName: "bag").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: "figure.walk.circle").renderingMode(.template)
Text("Steps")
})
.tag(2)
ProfileView()
.tabItem {
Image(systemName: "person")
Text("Profile")
}
.tag(3)
}
.accentColor(Color("ColorOnboarding"))
.navigationBarBackButtonHidden(true)
}
You can change image like this,
var body: some View {
ZStack {
TabView(selection:$selection) {
OffersView()
.tabItem ({
Image(systemName: selection == 1 ? "bag" : "bag2").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: selection == 2 ? "figure.walk.circle" : "figure.walk.circle2").renderingMode(.template)
Text("Steps")
})
.tag(2)
// -----Other Code-----
You can use state for it
var body: some View {
#State var isTapped = false
ZStack {
TabView(selection:$isTapped) {
OffersView()
.tabItem ({
Image(systemName: selection == 1 ? "bag" : "bag2").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: selection == 2 ? "figure.walk.circle" : "figure.walk.circle2").renderingMode(.template)
Text("Steps")
})
.tag(2)
ProfileView()
.tabItem {
Image(systemName: "person")
Text("Profile")
}
.tag(3)
}
.accentColor(Color("ColorOnboarding"))
.navigationBarBackButtonHidden(true)
}
So I created a tabview in SwiftUI but it keeps showing the 'three dots more' tab. How do I remove that?
TabView{
Text("")
.tabItem {
Image(systemName: "house")
Text("Home")
}
Text("")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Search")
}
Text("")
.tabItem {
Image(systemName: "plus")
Text("Add")
}
Text("")
.tabItem {
Image(systemName: "suit.heart")
Text("Likes")
}
Text("")
.tabItem {
Image(systemName: "person")
Text("Profile")
}
}//end tabview
Without more code it is not easy, to determine the reason for this behavior.
Check if the SceneDelegate window.rootcontroller points to your struct, where the TabView is located, not to the normal contentView
Replace the Textstrings with Views like:
TabView{
FirstView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
}
Check, that there is no Text between Tabview and the first TabItem, because the TabView interprets this as an additional Tabitem
TabView{
Text("this causes the error")
FirstView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
With these three tips your Tabbar should work
I'm working with Xcode 11.3