79512469

Date: 2025-03-16 11:05:55
Score: 5
Natty:
Report link

I changed @workingdogsupportUkraine code. The only thing is that it is not showing keyboard just by tapping on the search button. It is showing cancel button after I changed the code.

struct SearchbarView: View {
    @Binding var text: String
    @State private var showSearch: Bool = false
    
    var onSubmit: () -> Void
    
    var body: some View {
        VStack {
            HStack {
                Spacer()
                if showSearch {
                    SearchBar(text: $text, showSearch: $showSearch, onSubmit: onSubmit)
                        .frame(width: 350, height: 40)
                } else {
                    Image(systemName: "magnifyingglass")
                        .onTapGesture {
                            showSearch = true
                        }
                }
            }
        }
    }
}

struct SearchBar: UIViewRepresentable {
    @Binding var text: String
    @Binding var showSearch: Bool

    var onSubmit: (() -> Void)
    
    func makeUIView(context: Context) -> UISearchBar {
        let searchBar = UISearchBar()
        searchBar.isEnabled = true
        searchBar.searchBarStyle = .minimal
        searchBar.autocapitalizationType = .none
        searchBar.placeholder = "Search"
        searchBar.delegate = context.coordinator
        searchBar.setShowsCancelButton(true, animated: true)
        
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: Context) {
        uiView.text = text
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, UISearchBarDelegate {
        let parent: SearchBar
        
        init(_ parent: SearchBar) {
            self.parent = parent
        }
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            searchBar.showsCancelButton = true
            parent.text = searchText
        }
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            searchBar.resignFirstResponder()
            searchBar.showsCancelButton = true
            searchBar.endEditing(true)
            parent.onSubmit()
        }
        
        func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
            parent.text = ""
            searchBar.resignFirstResponder()
            searchBar.showsCancelButton = false
            searchBar.endEditing(true)
            parent.showSearch = false
        }
        
        func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
            searchBar.showsCancelButton = true
            return true
        }
    }
}

Please help me with keyboard focus and cancel should be highlighted after I tapped the search button. Now by tapping on cancel it also dismisses the search.

Reasons:
  • Blacklisted phrase (1): help me
  • RegEx Blacklisted phrase (3): Please help me
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @workingdogsupportUkraine
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: kjfhke