【问题标题】:SwiftUI Button not responding when added Padding to it向其添加填充时,SwiftUI 按钮没有响应
【发布时间】:2021-06-12 11:20:33
【问题描述】:
    struct ContactView: View {
    
    @Binding var isContactViewActive: Bool
    @State var searchBar = ""
    
    var backgroundColor = Color(red: 14/255, green: 18/255, blue: 23/255, opacity: 1.0)
    
    var body: some View {
        NavigationView {
            ZStack {
                backgroundColor
                VStack {
                    HStack {
                        Button(action: {}, label: {
                            Image(systemName: "magnifyingglass").font(.title)
                        })
                        Spacer()
                        Text("FireChat")
                            .font(.title)
                            .fontWeight(.light)
                            .foregroundColor(Color.white)
                        Spacer()
                        Button(action: {}, label: {
                            Image(systemName: "power").font(.title)
                        })
                    }.padding(.top, 50)
                    Spacer()
                }
            }.edgesIgnoringSafeArea(.all)
        }
    }
}

当我尝试在我的 HStack 周围添加 padding(.top, 50) 以将其定位得更低时,我的按钮在图像区域停止响应并且只能在上面的填充区域上单击。我不明白为什么会这样,如何在不影响按钮点击区域的情况下将 HStack 放低。

【问题讨论】:

  • 您分享的代码对我来说似乎运行良好。不知道我是否正确地回答了您的问题
  • 对我也很有效。您的代码中一定还有其他内容您没有向我们展示。
  • 好吧,如果我用 VStack 包装这个 HStack 并像这样添加一个 Spacer() 则它不起作用: VStack { HStack { ... 上面的代码 } Spacer() }
  • 对我来说很适合 VStack 和 Spacer。
  • 在哪里可以分享我所面临问题的视频?

标签: ios swift xcode swiftui


【解决方案1】:

要清楚这是我用于测试的代码:

    @main
    struct TestApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
struct ContactView: View {
    
    @Binding var isContactViewActive: Bool
    @State var searchBar = ""
    
    var backgroundColor = Color(red: 14/255, green: 18/255, blue: 23/255, opacity: 1.0)
    
    var body: some View {
        NavigationView {
            ZStack {
                backgroundColor
                VStack {
                    HStack {
                        Button(action: {print("magnifyingglass")}, label: {
                            Image(systemName: "magnifyingglass").font(.title)
                        })
                        Spacer()
                        Text("FireChat")
                            .font(.title)
                            .fontWeight(.light)
                            .foregroundColor(Color.white)
                        Spacer()
                        Button(action: {print("power")}, label: {
                            Image(systemName: "power").font(.title)
                        })
                    }.padding(.top, 50)
                }
            }.edgesIgnoringSafeArea(.all)
        }
    }
}

struct ContentView: View {
    @State var isContactViewActive = false
    var body: some View {
        ContactView(isContactViewActive: $isContactViewActive)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多