UISearchBar "X" 按钮未在某些视图上触发任何事件

UISearchBar "X" button not triggering any event on some views

我实现了一个简单的 UISearchBar,但是,当我直接调用搜索栏时,"X" 按钮才起作用。这是为什么?

这是我的代码:

UISearchBar

struct SearchBarView: View, UIViewRepresentable {
   @Binding var text: String

    class Coordinator: NSObject, UISearchBarDelegate {

        @Binding var text: String

        init(text: Binding<String>) {
            _text = text
        }

        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
        }
    }

    func makeCoordinator() -> SearchBarView.Coordinator {
        return Coordinator(text: $text)
    }

    func makeUIView(context: UIViewRepresentableContext<SearchBarView>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        searchBar.autocapitalizationType = .none
        searchBar.enablesReturnKeyAutomatically = true
        searchBar.showsSearchResultsButton = true
        searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        searchBar.searchTextField.backgroundColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
        searchBar.isTranslucent = true
        searchBar.placeholder = "Pesquisar"
        return searchBar
    }

    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBarView>) {
        uiView.text = text
    }
}

另一个文件中的我的 TopBarView

struct TopBarView: View {
    @EnvironmentObject var environmentController : EnvironmentController

    var body: some View {
        HStack() {
            Button(action: {
                self.environmentController.environment.showMenu.toggle()
            }) {
                Image(systemName: "line.horizontal.3")
                    .background(
                        RoundedRectangle(cornerRadius: 8.0)
                            .foregroundColor(Color.init(red: 239/255, green: 239/255, blue: 239/255))
                            .frame(width: 36, height: 36, alignment: .center))
                    .foregroundColor(.black)
                    .imageScale(.large)
                    .font(.subheadline)
                    .frame(width: 40, height: 40, alignment: .center)
            }
            SearchBarView(text: self.$environmentController.environment.searchText)
        }
        .padding(.top, 30)
        .padding(.all)
    }
}

我的MainView,也在另一个文件中

struct NextDeparturesView: View {
    @ObservedObject var userSettingsController = UserSettingsController()
    @EnvironmentObject var environmentController : EnvironmentController
    @State private var showMap = false

    var body: some View {
        VStack() {
            //If showUserActionSheet is false or showMap is true, display the menu button, the search bar and the map
            if(!self.userSettingsController.showUserCityActionSheet || showMap) {
                MapView()
                    .overlay(
                        TopBarView(),
                        alignment: .top)
                    .edgesIgnoringSafeArea(.top)
                    .onTapGesture {
                        if(self.environmentController.environment.showMenu) {
                            self.environmentController.environment.showMenu.toggle()
                        }
                }
            }
        }

因此,如果在我的 MainView 中直接调用 SearchBarView,"x" 按钮会起作用。但是,如果我使用 TopBarView,如代码中所示,"x" 按钮将停止工作。可能还值得一提的是,在搜索栏中写入始终有效,只是停止工作的 "x" 按钮。

我是 swift 的新手,我错过了什么?

谢谢

编辑

我还是想不通,但我想它在覆盖层内时会停止工作。在我的 MainView 中,如果我将 TopBarView() 调用移到叠加层之外,它就可以工作。

EDIT2

所以,如果我将 allowHitTesting(false) 添加到我的 MainView 叠加层,UISearchBar 开始工作,但是,菜单按钮停止...如果您的叠加层中只有一个操作,这可能是适合您的解决方案.

好的,所以如果有人发现自己和我处于相同的位置并且找不到解决方案,我让它工作的唯一方法就是放弃覆盖并使用 ZStack 开发更复杂的视图。

在问题编辑中你可以看到我之前尝试过的,也许对你有帮助。

我会接受这个作为答案,但是,如果有人最终想出更好的解决方案,我会改变它。