【问题标题】:UISearchBar "X" button not triggering any event on some viewsUISearchBar“X”按钮不会在某些视图上触发任何事件
【发布时间】:2020-06-02 22:28:20
【问题描述】:

我已经实现了一个简单的 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 开始工作,但是,菜单按钮停止...如果您的叠加层中只有一个操作,这可能是您的解决方案。

【问题讨论】:

    标签: ios swift swiftui uisearchbar xcode11


    【解决方案1】:

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

    在问题编辑中您可以看到我之前尝试过的内容,也许它会对您有所帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多