A searchBar for SwiftUI implemented with a UITextField which is the first responder.
- iOS 13.0+
- Xcode 11.0+
- Swift 5.0+
- Select
File > Swift Packages > Add Package Dependency...
. - Enter
https://github.com/struuuuggle/SwiftUISearchBar
in the "Choose Package Repository" dialog. - Specify a rule as
Branch
, and set the branch field tomaster
.
After installation, you can import SwiftUISearchBar to your project by adding the following sentence.
import SwiftUISearchBar
Then you can use SwiftUISearchBar
in your project like this.
@State var searchTerm: String = ""
var body: some View {
SwiftUISearchBar(text: $searchTerm, placeholder: "Search", onTap: {
// do something on tapping the textField
}, onEdit: {
// do something on editing the textField.text
}, onCommit: { textFieldText in
// do something on tapping the search button in keyboard
}, onCancel: {
// do something on tapping the cancel button
})
}
See also Demo.