how to switch tab programmatically on button click? in swiftui - ios

I have implemented tab bar in my code. I have see all button in my first tab and from that button i want to switch to second tab programmatically. When I use navigationView then it creates another tab bar and moves to that screen and this changes the index of navigation in swiftui.
struct AppTabNavigation: View {
#State var selection: Tab = .dashboard
var body: some View {
TabView(selection: $selection) {
NavigationView {
FirstTabView()
}.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
Label("Home", systemImage: "house.fill")
.accessibility(label: Text("Home"))
}
.tag(Tab.home)
NavigationView {
SecondView()
}.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
Label("Home", systemImage: "house.fill")
.accessibility(label: Text("Home"))
}
.tag(second)
}
}
}
Navigation Code:
NavigationLink(destination: AppTabNavigation(selection: Tab.home), isActive: self.$isActiveTabbar){
Text("")
} .isDetailLink(false)

Here is a demo of possible approach - the idea is to move binding for tab selection into view with buttons, so button action could change it.
Tested with Xcode 12 / iOS 14
enum Tab {
case dashboard
case home
case second
}
struct AppTabNavigation: View {
#State var selection: Tab = .home
var body: some View {
TabView(selection: $selection) {
NavigationView {
FirstTabView(tab: $selection)
}.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
Label("Home", systemImage: "house.fill")
.accessibility(label: Text("Home"))
}
.tag(Tab.home)
NavigationView {
Text("SecondView")
}.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
Label("Home", systemImage: "house.fill")
.accessibility(label: Text("Home"))
}
.tag(Tab.second)
}
}
}
struct FirstTabView: View {
#Binding var tab: Tab
var body: some View {
Button("Go Second") { self.tab = .second }
}
}

Related

SwiftUI Toolbar, Tabview & TabBarItem is not behaving as I would expect

When adopting NavigationStack with a TabView embedded, then using a Toolbar and ToolbarItems, I get unexpected behaviour when switching between tabs.
Expectation: I should be able to have different ToolbarItems depending on which tab is selected and when switching between tabs, the ToolbarItem in the position I specify per tab should change.
Behaviour: The ToolbarItems append to the specified position as you move across each tab.
#State var selectedIndex: Int
#State var path = NavigationPath()
var body: some View {
ZStack {
NavigationStack(path: $path) {
TabView(selection: $selectedIndex) {
Group {
TextView(tabName: .constant("Home"))
.tabItem {
Label("Home", systemImage: "house")
}
.tag(1)
TextView(tabName: .constant("Contacts"))
.tabItem {
Label("Contacts", systemImage: "person.3")
}
.tag(1)
TextView(tabName: .constant("Settings"))
.tabItem {
Label("Settings", systemImage: "gearshape")
}
.tag(1)
}
.toolbarBackground(Color.red)
.toolbarBackground(.visible, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
}
}
}
}
}
And for the view for each tab (this is a simplified example, I have tried using seperate views but get the same behaviour);
#Binding var tabName: String
var body: some View {
Text("Tab \(tabName)")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
print("Profile ")
} label: {
Image(systemName: "person.circle")
}
}
}
}
}
Screenshot of Tab 2
I can't find any information in the developer docs on how to resolve this issue.

add an exit button to .navigationBarItems

I'm a beginner at swiftui. I need to add an exit button to .navigationBarItems. How can I add this button in the parent NavigationView to show this button on all children's views?
// a simple example on my question
struct FirstView: View {
var body: some View {
NavigationView {
ZStack{
TabView{
SubExampleViewOne()
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
SubExampleViewTwo()
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("Bookmark")
}
}
}
//here I have added a toolbar and it is perfectly visible in tabitem
//this is what I am trying to achieve, the visibility of the button on all pages
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
ButtonExitView()
}
}
}
}
}
something strange - if I add NavigationLink in this way, Image and Text("Home") are visible twice
and the ToolbarItem is no longer on the new page
struct SubExampleViewOne: View {
var body: some View {
Text("This is hime page!")
.padding()
NavigationLink(destination: SubExampleViewThree()){
Text("Navigation link")
}
}
}
struct SubExampleViewTwo: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct SubExampleViewThree: View {
var body: some View {
Text("This is Navigation link")
.padding()
}
}
struct ButtonExitView: View {
var body: some View {
Button(action: {}, label: {Image(systemName: "arrowshape.turn.up.right.circle")})
}
}
after learning about TabView, I thought that there should be a similar solution for the top of the page
You have to add the button to each child view separately.
And you should use .toolbar and .toolBarItem because .navigationBarItems is deprecated.

TabView does not work correctly on iOS13 SwiftUI

I created a TabView with 4 items but with iOS 13.x only the first view is displayed correctly.
When I click on another item's icon, the view is not shown correctly but the app only shows a white view. If I run the app on iOS > 14 I can correctly view all the views.
TabView implementation:
struct ContentView: View {
private enum Tab: Hashable {
case discovery
case qrcode
case devices
case settings
}
#State private var selectedTab: Tab = .discovery
var body: some View {
NavigationView {
TabView(selection: $selectedTab) {
DiscoveryView()
.tabItem {
VStack {
Image(systemName: "lock.rotation.open")
Text("Discovery")
}
}
.tag(0)
QrCodeView()
.tabItem {
VStack {
Image(systemName: "qrcode.viewfinder")
Text("QrCode")
}
}
.tag(1)
DevicesView()
.tabItem {
VStack {
Image(systemName: "qrcode.viewfinder")
Text("My devices")
}
}
.tag(2)
SettingsView()
.tabItem {
VStack {
Image(systemName: "gear")
Text("Settings")
}
}
.tag(3)
}
}
}
Implementation of one of the views:
struct QrCodeView: View {
var body: some View {
Text("QrCode")
}
}
Where am I doing wrong?
It might be a reason of selection... selection and tag types should be the same, so try
#State private var selectedTab: Tab = .discovery
var body: some View {
NavigationView {
TabView(selection: $selectedTab) {
DiscoveryView()
.tabItem {
VStack {
Image(systemName: "lock.rotation.open")
Text("Discovery")
}
}
.tag(.discovery) // << here !!
QrCodeView()
.tabItem {
VStack {
Image(systemName: "qrcode.viewfinder")
Text("QrCode")
}
}
.tag(.qrcode) // << here !!
// ... others the same

TabView in SwiftUI not responding

I am trying to make an app using the TabView. The app renders and runs nicely, except for the fact that tapping on the tabs does nothing.
Here is my code, am I missing something?
TabView {
HomeView()
.tabItem {
VStack {
Image(systemName: "1.circle")
Text("Home")
}
}.tag(1)
SecondView()
.tabItem {
VStack {
Image(systemName: "2.circle")
Text("SecondView")
}
}.tag(2)
}
I had the same issue, in the end it turned out I had the accessibility option "Full keyboard access" turned on. Switching this off fixed it.
Here is a minimal example which works fine for me:
struct HomeView: View {
var body: some View {
Text("Home")
}
}
struct SecondView: View {
var body: some View {
Text("SecondView")
}
}
struct ContentView: View {
var body: some View {
TabView {
HomeView()
.tabItem {
VStack {
Image(systemName: "1.circle")
Text("Home")
}
}.tag(1)
SecondView()
.tabItem {
VStack {
Image(systemName: "2.circle")
Text("SecondView")
}
}.tag(2)
}
}
}
I hope this helps!

How can I fix the navigation bar to go to the top of the screen in a tabView?

I'm trying to create a TabView that contains a NavigationView. However, the navigation bar does not reach the top of the screen. How can I fix this?
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
TestView()
}
.tabItem {
Image(systemName: "star")
Text("Tab 1")
}
Text("Tab 2")
.tabItem {
Image(systemName: "star")
Text("Tab 2")
}
Text("Tab 3")
.tabItem {
Image(systemName: "star")
Text("Tab 3")
}
}
}
}
struct TestView: View {
var body: some View {
List {
Text("Hello")
}
.navigationBarTitle("Title")
}
}
The following view modifier will help you
.edgesIgnoringSafeArea(.top)

Resources