Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

popping a view makes the previous view non clickable #12

Closed
divyanshunegi opened this issue Mar 16, 2020 · 4 comments
Closed

popping a view makes the previous view non clickable #12

divyanshunegi opened this issue Mar 16, 2020 · 4 comments

Comments

@divyanshunegi
Copy link

I have a home screen with 3 Tabs one of the tab has scroll view with some clickable items, on pushing this navigation view stack, and popping back the view remains non-clickable unless the scroll is moved a little and then everything is back to clickable again.

HELP!

@matteopuc
Copy link
Owner

Hi @divyanshunegi, thanks for asking. I'd be really happy to help you debugging this issue, would you be able to provide me with a minimum example that causes the problem? I'm asking because I tried to reproduce this issue by myself, but I didn't manage to. Take a look at this silly example below which exploits a scroll view and the navigation stack:

import SwiftUI
import NavigationStack

struct ContentView : View {
    var body: some View {
        NavigationStackView {
            View1()
        }
    }
}

struct View1: View {
    var body: some View {
        ZStack {
            Color.green.edgesIgnoringSafeArea(.all)

            ScrollView {
                ForEach(0..<20, id: \.self) { _ in
                    ScrollViewRow()
                }
            }
        }
    }
}

struct ScrollViewRow: View {
    var body: some View {
        PushView(destination: View2()) {
            Text("PUSH")
        }
        .frame(width: UIScreen.main.bounds.size.width, height: 200)
        .background(Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)))
    }
}

struct View2: View {
    var body: some View {
        ZStack {
            Color.yellow.edgesIgnoringSafeArea(.all)
            
            PopView {
                Text("POP")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Mar-16-2020 14-47-37

it seems that everything is fine. I guess it's something related to your specific case. Let's try to get what's happening together.
Matteo

@divyanshunegi
Copy link
Author

divyanshunegi commented Mar 17, 2020

Mar-17-2020 16-30-12

Here in this GIF you can see, I have created a custom TApBar which shows the screens as per a Binding value count, when I am in contact page and enter any Navigation view, and pop back, My tapbar shows its value in the center which is not its original state before entering the navigation.

This is somewhat related to the non clicky behaviour I observed in my production app which I cannot share here due to NDA 😵

@matteopuc

@divyanshunegi
Copy link
Author

This is contentView

        NavigationStackView{
            ZStack(alignment: .bottom){
                if(self.selectedTab == 0){
                    HomeTab().background(Color.blue)
                } else if(self.selectedTab == 1){
                    ContactTab(title: "Contacts").background(Color.green)
                } else if(self.selectedTab == 2){
                    ContactTab(title: "Profile").background(Color.red)
                }
                
                VStack{
                    Spacer()
                    CustomTabView(selectedTab: $selectedTab).padding()
                }
            }.edgesIgnoringSafeArea([.top,.bottom])
        }

and this is the scroll view code :

        ScrollView{
            VStack {
                PushView(destination: ChildView(title: "Child 1").background(Color.red)) {
                    Text("1")
                        .font(.largeTitle)
                }
                
                PushView(destination: ChildView(title: "Child 2").background(Color.red)) {
                    Text("2")
                        .font(.largeTitle)
                }
                
                PushView(destination: ChildView(title: "Child 3").background(Color.red)) {
                    Text("3")
                        .font(.largeTitle)
                }
                
                PushView(destination: ChildView(title: "Child 4").background(Color.red)) {
                    Text("4")
                        .font(.largeTitle)
                }
                
                PushView(destination: ChildView(title: "Child 5").background(Color.red)) {
                    Text("5")
                        .font(.largeTitle)
                }
                
                PushView(destination: ChildView(title: "Child 6").background(Color.red)) {
                    Text("6")
                        .font(.largeTitle)
                }
                
            }.padding(.top,100)
            
        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)

@matteopuc
Copy link
Owner

Here in this GIF you can see, I have created a custom TApBar which shows the screens as per a Binding value count, when I am in contact page and enter any Navigation view, and pop back, My tapbar shows its value in the center which is not its original state before entering the navigation.

This is somewhat related to the non clicky behaviour I observed in my production app which I cannot share here due to NDA 😵

@matteopuc

Hi @divyanshunegi, sorry for the late reply. What you are observing in this GIF is explained in the Issues section of the README file: when a view is removed from a view hierarchy the State gets reset. The State property you use to set the bar gets reset during the navigation. In order to avoid this (which can't be fixed in the current version of SwiftUI) you have to create a view model and put the State property there instead of having it as a State property in your view. You have to change this (just an example):

struct ContentView : View {
    @State private var myStateVar = 0

    var body: some View {
        MyBar($myStateVar)
    }
}

To this:

class StateViewModel: ObservableObject {
    @Published var myStateVar = 0
}
struct ContentView : View {
    @ObservedObject var stateViewModel = StateViewModel()

    var body: some View {
        MyBar($stateViewModel.myStateVar)
    }
}

Matteo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants