Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding buttons inside UISearchBar

enter image description here

I want to add a button (bar) inside a UISearchbar and another one just outside the UISearchbar as shown in the image. Any help on this appreciated.

Thanks in advance Naveen

like image 700
Naveen Thunga Avatar asked Apr 09 '14 14:04

Naveen Thunga


2 Answers

Edit :

As stated by @NicolasMiari in the comments :

This no longer works post-iOS 7, since the bookmarks button is rendered inside the bar's input text field.


For the button inside the search bar, you can use the bookmark button and change its image. You simply go to your storyboard (if you use one), select the search bar, and activate the option "Shows Bookmarks Button". Then, in your code, set the image you want :

[_searchBar setImage:[UIImage imageNamed:@"My-Custom-Image"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];

You can detect a click on this button with the following delegate method :

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {

    NSLog(@"click");
}
like image 64
rdurand Avatar answered Oct 13 '22 00:10

rdurand


Swift 4

class ViewController: UIViewController {

   var searchController = UISearchController(searchResultsController: nil)

   override func viewDidLoad() {
      searchController.delegate = self 
      searchController.searchBar.delegate = self 
      searchController.searchBar.showsBookmarkButton = true
      searchController.searchBar.setImage(UIImage(named: "myImage"), for: .bookmark, state: .normal)
   }

}

extension ViewController: UISearchBarDelegate {

   func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
      print("click")
   }

}
like image 29
Strat Aguilar Avatar answered Oct 13 '22 01:10

Strat Aguilar