meep
meep

Reputation: 31

Buttons in a subview not working with SwiftUI

I have this subview declared here:

import SwiftUI

struct NavigationBarView: View {
        
    @State private var showingMenu = false

    var body: some View {
        HStack {
            Button(action: {print("test")}, label: {
                Image(systemName: "line.horizontal.3")
                    .font(.title)
                    .foregroundColor(.white)
            })
            Spacer()
            Text("SAT Daily")
                .font(.custom("Nunito-Bold", size: 30))
                .foregroundColor(.white)
                .onTapGesture {
                    print("test")
                }
            Spacer()
            Button(action: {print("test1")}, label: {
                Image(systemName: "person.circle")
                    .font(.title)
                    .foregroundColor(.white)
            })
        }.onTapGesture {
            print("bruh")
        }
    }
}

I call this view in my homepage to display it (the view above is a header):

    var body: some View {
        NavigationView {
            ZStack {
                Color(UIColor(red: 0.067, green: 0.137, blue: 0.322, alpha: 1))
                    .ignoresSafeArea()
                    VStack() {
                        
                        NavigationBarView()
                            .padding(.horizontal, 15)
                            .padding(.bottom)
                            .padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
                            .background(Color(UIColor(red: 0.024, green: 0.092, blue: 0.267, alpha: 1).cgColor))
                            .zIndex(1)
                        
                        ScrollView(/*@START_MENU_TOKEN@*/.vertical/*@END_MENU_TOKEN@*/, showsIndicators: false, content: {

If I click any of the buttons that print the statements correspond to it does not print. How do I fix this?

The profile icon and menu icon are the buttons that don’t work. The header (with the logo SAT Daily) is the sub view that is being called on my main page.

https://i.sstatic.net/3Qvo1.png

Upvotes: 1

Views: 1302

Answers (1)

Taeeun Kim
Taeeun Kim

Reputation: 1256

If you click the buttons on the simulator(not in Preview), it will print out. (Your code runs fine, when I run it)

enter image description here

Upvotes: 1

Related Questions