Search code examples
swiftuiswiftui-navigationlink

view not displaying correctly because of NavigationView


i added an image to show the issue

im not sure why the view is being "shrinked"

im pretty sure the issue is the NavigationStack, however, when i select an image i get kicked back to the previous screeen.

when i select an image, i get kicked back to the previous screen. these are the components::

COMPONENT 1::

//MainView

import SwiftUI
import Firebase // Import Firebase module

struct MainView: View {
    @StateObject var authManager = AuthService()

    var body: some View {
        NavigationView{
            AuthView()
            
        }
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}
COMPONENT2::
import SwiftUI

struct AuthView: View {
    var body: some View {
            VStack {
                Text("Welcome to MyApp")
//                    .font(.title)
//                    .padding()

                Spacer()

                NavigationLink(destination: ProfileEditView()) {
                    Text("Sign Up")
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(8)
                }

                Spacer()

                Button(action: {
                    // Handle login action
                    print("Login tapped")
                }) {
                    Text("Login")
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.green)
                        .cornerRadius(8)
                }
                .padding()
            }
            .padding()
            .navigationBarTitle("") // Empty title to remove default navigation bar space
            .navigationBarHidden(true) // Hide the navigation bar
            }
}



struct AuthView_Previews: PreviewProvider {
    static var previews: some View {
        AuthView()
    }
}
COMPONENT3::
import SwiftUI

struct ProfileEditView: View {
    @State private var name: String = ""
    @State private var bio: String = ""
    @State private var selectedImages: [UIImage] = []
    @State private var gender: Gender = .male
    @State private var selectedInterestedSex: Sex = .male

    var body: some View {
      
            Form {
                Section(header: Text("Profile Information")) {
                    TextField("Name", text: $name)
                    TextField("Bio", text: $bio)
                    
                    ImagePicker(images: $selectedImages)
                        .frame(height: 100) // Adjust the height as needed

                    Picker("Gender", selection: $gender) {
                        ForEach(Gender.allCases, id: \.self) { gender in
                            Text(gender.rawValue).tag(gender)
                        }
                    }

                    Picker("Interested In", selection: $selectedInterestedSex) {
                        ForEach(Sex.allCases, id: \.self) { sex in
                            Text(sex.rawValue).tag(sex)
                        }
                    }
                }

                Section {
                    Button("Save") {
                        // Add code to save profile information
                    }
                    .disabled(name.isEmpty || bio.isEmpty || selectedImages.isEmpty)
                }
            }
            .navigationTitle("Edit Profile")
        
    }
}

struct ImagePicker: View {
    @Binding var images: [UIImage]
    @State private var isImagePickerPresented: Bool = false

    var body: some View {
        VStack {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 10) {
                    ForEach(images, id: \.self) { image in
                        Image(uiImage: image)
                            .resizable()
                            .scaledToFit()
                            .frame(width: 80, height: 80)
                    }
                }
            }

            Button("Add Image") {
                isImagePickerPresented.toggle()
            }
            .sheet(isPresented: $isImagePickerPresented) {
                ImagePickerView(images: $images, isPresented: $isImagePickerPresented)
            }
        }
    }
}

struct ImagePickerView: UIViewControllerRepresentable {
    @Binding var images: [UIImage]
    @Binding var isPresented: Bool

    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }

    func makeUIViewController(context: Context) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        picker.sourceType = .photoLibrary
        picker.allowsEditing = true
        return picker
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}

    class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        let parent: ImagePickerView

        init(parent: ImagePickerView) {
            self.parent = parent
        }

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
            if let image = info[.editedImage] as? UIImage {
                parent.images.append(image)
            }

            parent.isPresented = false
        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            parent.isPresented = false
        }
    }
}

enum Gender: String, CaseIterable {
    case male = "Male"
    case female = "Female"
}

enum Sex: String, CaseIterable {
    case male = "Male"
    case female = "Female"
    case both = "Both"
}

struct ProfileEditView_Previews: PreviewProvider {
    static var previews: some View {
        ProfileEditView()
    }
}

enter image description here

im looking at this video but i still cant see what is wrong, i could not find a stackoverflow similar issue


Solution

  • You should only have 1 NavigationView or NavigationStack. It looks like you have multiple so remove any additional.