I am writing an application using Xcode 14.0.1, and testing on an iPhone 12 mini running iOS 16.0. The current project build is for iOS 14.7. Here is my TabView...
TabView {
ByEyeView()
.tabItem { Label("ByEye", systemImage: "eye") }
ChartView()
.tabItem { Label("Chart", systemImage: "square.grid.4x3.fill") }
ListView()
.tabItem { Label("List", systemImage: "list.bullet") }
EditView()
.tabItem { Label("Edit", systemImage: "square.and.pencil") }
CameraView()
.tabItem { Label("Camera", systemImage: "camera") }
SettingsView()
.tabItem { Label("Settings", systemImage: "gear") }
}
//.labelStyle(TitleAndIconLabelStyle())
//.padding(8)
//.ignoresSafeArea(edges: .bottom)
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
This gives a capsule at the bottom of the page with a small version of the icon and no text. I cannot enlarge the Label with .frame, and the .labelStyle() setting is ignored. I take it this is part of PageTabViewStyle() - the index is supposed to be small, and I can probably not change that. But the index sits over the view content, so I need its height if I am to keep buttons clear of it.
Can I find out the index height? Or does PageTabViewStyle assume that the index is small and you should work around it?
The commented-out .ignoreSafeArea() moves the index down while the page remains the same. The .padding() keeps it a bit clear of the bar at the bottom. This is what I am working with for now. This is foul: it will not work with other devices or screen orientations.
The bigger picture:
I have six entries. That does not fit in the default view, so I get a ... More tag which leads to an extra menu. Ugly. I like the PageTabViewStyle method of scrolling, but I want an index with a known height - preferably one that uses the full labels and sits at the bottom of the TabView layout, under the tabbed views.
This was one of those 'Magic Eye' things when you stare at it for days and it makes no sense, and suddenly everything rearranges itself...
Maybe TabPageViewStyle was intended to be for pages where there is no visible index, or overlaying a small index does no harm. This would work for browsing images. All the cunning has gone into making the index view unobtrusive. If you need to know how big it is, then perhaps TabPageViewStyle is not what you want.
What I said I wanted was actually a Scrollable horizontal list, followed by the currently selected list. Something like this...
let tabW = CGFloat(UIScreen.main.bounds.width / 5.0)
enum Page {
case ByEye
case Chart
case List
case Edit
case Camera
case Settings
}
#State private var page = Page.ByEye
func pageButton(_ select: Page, _ icon: String, _ title: String) -> some View {
return Button {
page = select
} label: {
VStack {
Image(systemName: icon)
Text(title)
} .frame(width: tabW)
} .foregroundColor( page == select ? Color.white : Color.gray )
}
var body: some View {
VStack() {
ScrollView(.horizontal) {
HStack() {
pageButton(Page.ByEye, "eye", "ByEye")
pageButton(Page.Chart, "square.grid.4x3.fill", "Chart")
pageButton(Page.List, "list.bullet", "List")
pageButton(Page.Edit, "square.and.pencil", "Edit")
pageButton(Page.Camera, "camera", "Camera")
pageButton(Page.Settings, "gear", "Settings")
}
}
switch page {
case .ByEye:
ByEyeView()
case .Chart:
ChartView()
case .List:
ListView()
case .Edit:
EditView()
case .Camera:
CameraView()
case .Settings:
SettingsView()
}
Spacer()
}
It is not much longer than my original version. It is not as pretty is it could be - when you overflow the title bar you get half an icon, where an ellipsis would be better. But I can fix that later.
The other answer is to write your own index table....
ScrollViewReader { proxy in
ScrollView(.horizontal, showsIndicators: false) {
HStack() {
pageButton(Page.EyeTest, "eyeglasses", "EyeTest", proxy)
pageButton(Page.Tone, "pause.rectangle", "Tone", proxy)
pageButton(Page.Chart, "square.grid.4x3.fill", "Chart", proxy)
pageButton(Page.ByEye, "eye", "ByEye", proxy)
pageButton(Page.List, "list.bullet", "List", proxy)
pageButton(Page.Camera, "camera", "Camera", proxy)
pageButton(Page.Settings, "gear", "Settings", proxy)
}
}
.onAppear { proxy.scrollTo(page, anchor: .center) }
.onChange(of: page) { page in
withAnimation {
proxy.scrollTo(page, anchor: .center)
}
}
}
This particular one has button-sized icons and text. 'page' is an enum, and also the tags of the TabView. If you stick it in the layout, you can make it fit around the TabView. You will want to hide the TabView index, which you can do with...
.tabViewStyle(PageTabViewStyle(indexDisplayMode:.never))
I have a tabView and I'm trying to change it's color. Using accentColor(:_) works but it's going to be deprecated.
TabView {
AppetizerListView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
AccountView()
.tabItem {
Image(systemName: "person")
Text("Account")
}
OrderView()
.tabItem {
Image(systemName: "bag")
Text("Order")
}
}
.accentColor(Color("brandPrimary"))
Instead I've tried to use .tint(:_) as Apple suggests but is not working (it builds but does not change the color).
TabView {
AppetizerListView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
AccountView()
.tabItem {
Image(systemName: "person")
Text("Account")
}
OrderView()
.tabItem {
Image(systemName: "bag")
Text("Order")
}
}
.tint(Color("brandPrimary"))
I also tried using .tint(_:) in each TabItem but it's also not working.
Any idea of what's going on or which is the correct way of making my code work as expected without using deprecated functions?
Maybe I'm using tint in a wrong way
Thanks!
I've found the solution to the problem, but I'll leave the post here for anyone who has the same problem.
What you have to do is to go to the Assets folder and define the AccentColor (that it has to be already created) as the color that you want your bar to be.
No modifiers have to be added to the tabView and it will automatically be showing the tabView with the color you defined as the AccentColor in your Assets folder.
How can you dynamically change SwiftUI Navigation Bar Items?
I have a TabView within a NavigationView, and I would like the Navigation Bar Items to change depending on tab that is selected. However, I am having a hard time determining how to change this with .onAppear(), assuming that is even what you are suppose to do.
My code is currently laid out as follows:
var body: some View {
NavigationView {
TabView {
contentWithNavigationButtons()
.tabItem {
Image(systemName: "house")
Text("Buttons")
}
contentWithoutNavigationButtons()
.tabItem {
Image(systemName: "person")
Text("No Buttons")
}
.onAppear {
//Navigation Bar Items should be removed
}
}
.navigationBarItems(trailing: someButton)
}
Here is a demo of possible solution - add tracking selection for tabs and make button depending of tab selection. Tested with Xcode 12 / iOS 14
struct DemoView: View {
#State private var selection = 0 // << here !!
var body: some View {
NavigationView {
TabView(selection: $selection) {
contentWithNavigationButtons()
.tabItem {
Image(systemName: "house")
Text("Buttons")
}.tag(0) // << here !!
contentWithoutNavigationButtons()
.tabItem {
Image(systemName: "person")
Text("No Buttons")
}.tag(1)
}
.navigationBarItems(trailing: Group {
if selection == 0 { // << here !!
Button("Some"){}
}
})
}
}
}
I've noticed that in my app .edgesIgnoringSafeArea renders my view differently in iOS 13.3 vs. iOS 13.4.
In my ContentView I have a modifier of .edgesIgnoringSafeArea(.top) applied. This displayed correctly in all iOS 13 versions leading up to 13.4. Now in the GM of 13.4 the top and bottom of the view is getting cut off.
Here's my ContentView
struct ContentView: View {
#EnvironmentObject var session: SessionStore
func getUser() {
session.listen()
}
var body: some View {
Group {
ZStack {
TabView {
ExploreView().tabItem {
Image(systemName: "house.fill")
Text("Explore")
}.tag(1)
FestivalsView().tabItem {
Image(systemName: "globe")
Text("Festivals")
}.tag(2)
ProfileView().tabItem {
Image(systemName: "person.crop.circle.fill")
Text("Profile")
}.tag(3)
}
.accentColor(Color("wdwPurple"))
.edgesIgnoringSafeArea(.top)
}
}.onAppear(perform: getUser)
}
}
Here's how it displays:
Any ideas?
In fact, on iOS GM 13.4 look like is correct, because is ignore top safe area
I removed the modifier and it seemed to display correctly. Like other people have said, the simulator isn't a great indicator of how things actually render on an actual device.
I have a TabView in SwiftUI with tabs. When I scroll list from one FirstView and tap another tab, and switch back to FirstView, my List in FirstView automatically redraws and scrolls to top. How to fix that.
enter link description here
This is FirstView
var body: some View {
NavigationView {
List {
ForEach(feed) { game in
FeedCardItem(gameModel: game)
.frame(width: UIScreen.main.bounds.width - 30, height: 400)
}
}
.navigationBarTitle(Text("Новое сегодня"))
}
}
This is TabView implementation
TabView (selection: $currentTab) {
FeedView().tabItem {
Image(systemName: currentTab == 0 ? "house.fill" : "house")
.renderingMode(.original)
.imageScale(.large)
}.tag(0)
RecommendationsView().tabItem {
Image(systemName: currentTab == 1 ? "gamecontroller.fill" : "gamecontroller")
.renderingMode(.original)
.imageScale(.large)
}.tag(1)
SearchView().tabItem {
Image(systemName: currentTab == 2 ? "flame.fill" : "flame")
.renderingMode(.original)
.imageScale(.large)
}.tag(2)
NotificationsView().tabItem {
Image(systemName: currentTab == 3 ? "bell.fill" : "bell")
.renderingMode(.original)
.imageScale(.large)
}.tag(3)
ProfileView().tabItem {
Image(systemName: currentTab == 4 ? "person.fill" : "person")
.renderingMode(.original)
.imageScale(.large)
}.tag(4)
}.edgesIgnoringSafeArea(.top)
}
I don't think it is possible via SwiftUI. The alternate to do this is to create the tabview in UIKit and use it in SwiftUI.
I have created a quick sample of how this can be achieved and how it solves your issue. Let me know if you have any other questions.
I have uploaded the code to GitHub.
https://github.com/subhan5/TabTest
In the uploaded code, the folder UITab contains the tabview created via UIKit.
In ContentView, I convert the same as UIViewControllerRepresentable and use it.
I have the same problem, and found this solution: https://kavsoft.dev/SwiftUI_2.0/Tab_Bar
Basically, you could just hide the content of one tab instead of replace it with another content view. I thought this solution is a little tricky but works.
it is possible with the UITableViewConfigurator which is described here: Scroll SwiftUI List to new selection
But of course...this uses UIViewControllerRepresentable....