Skip to content

YagbhanSingh/SearchBarView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” SearchBarView – Customizable UIKit Search Bar in Swift

SearchBarView is a fully customizable and reusable UIKit-based search bar component written in Swift. It allows iOS developers to integrate a feature-rich search interface into their apps with minimal setup. Designed for flexibility and style, it supports advanced UI styling, tap actions, debounce handling, and more.

✨ Features

  • Custom placeholder text, font, and color
  • Optional cancel button with animation
  • Configurable height and paddings
  • Border styling and background customization
  • Left and right icon support with tap actions
  • Built-in debounce handling for efficient search
  • Public methods to control and access state

πŸ“Έ Preview

(Include a screenshot or GIF here)

πŸ“¦ Installation

Copy SearchBarView.swift to your project. No external dependencies required.

πŸ› οΈ Usage

1. Add the View

let searchBar = SearchBarView(
    placeholder: "Search users",
    showCancelButton: true,
    backgroundColor: .white,
    searchTextFieldColor: .systemGray6,
    borderColor: .lightGray,
    cornerRadius: 12,
    addShadow: true,
    height: 48,
    padding: UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16),
    placeholderFont: UIFont.systemFont(ofSize: 14),
    placeholderTextColor: .darkGray,
    leftIcon: UIImage(systemName: "magnifyingglass"),
    rightIcon: UIImage(systemName: "mic"),
    onLeftIconTap: {
        print("Left icon tapped")
    },
    onRightIconTap: {
        print("Right icon tapped")
    }
)
searchBar.delegate = self
view.addSubview(searchBar)

// Add layout constraints or frame manually

2. Implement the Delegate

extension YourViewController: SearchBarViewDelegate {
    func searchBarView(_ searchBarView: SearchBarView, didChangeText text: String) {
        print("Search Text: \(text)")
    }

    func searchBarViewDidCancel(_ searchBarView: SearchBarView) {
        print("Search cancelled")
    }
}

πŸ”§ Public API

func setText(_ text: String)
func getText() -> String
func clearText()
func become()   // Focus textfield
func resign()   // Remove focus
func setCancelButtonVisible(_ visible: Bool, animated: Bool = true)

⏳ Debounce

Search text changes are debounced (default: 0.3s) to avoid frequent firing. You can customize it:

SearchBarView(debounceInterval: 0.5)

πŸ“Œ Customizations

Property Description
placeholder Placeholder string
showCancelButton Shows or hides cancel button
padding Outer padding (UIEdgeInsets)
backgroundColor Search view background
searchTextFieldColor Text field background color
borderColor Border color
borderWidth Border width
cornerRadius Corner radius for text field
height Overall height
placeholderFont Custom placeholder font
placeholderTextColor Placeholder text color
textfieldFont Main text font
textfieldTextColor Text field color
leftIcon / rightIcon Icons on either side
onLeftIconTap, onRightIconTap Actions for icon taps

βœ… Requirements

  • iOS 13+
  • Swift 5+

🀜 Contributions

Feel free to fork, improve or submit issues! Your feedback is welcome.

πŸ§‘β€πŸ’» Author

Your Name πŸ“§ chaudharyyagh@gmail.com πŸ”— @yagbhan-singh

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published