Muhammad Hewedy
Muhammad Hewedy

Reputation: 30078

Add button to the right of uisearch bar located at navigationItem's titleView

I've a UISearchBar in navigationItem's titleView:

self.searchBar = ....
self.navigationItem.titleView = self.searchBar;

And I need to add a button just right the UISearchbar and to be hidden when the search bar gain focus. (i.e. when the cancel button appears).

How to accomplish such behavior? enter image description here

Thanks.

Upvotes: 3

Views: 1442

Answers (1)

Nikolay Apostolov
Nikolay Apostolov

Reputation: 69

What you need to do is creating another UIView and put the UISearchBar and your UIButton "Settings" right to it. Then add the new composite UIView instead of self.searchBar directly.

Here is an example:

UISearchBar *_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 230, SEARCHBAR_HEIGHT)];
    _searchBar.barStyle = UIBarStyleBlack;
    _searchBar.delegate = self;
UIButton *_button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, BUTTON_HEIGHT)];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, SEARCHBAR_WIDTH, SEARCHBAR_HEIGHT)];
    [searchBarView addSubview:_searchBar];
    [searchBarView addSubview:_button];
    self.navigationItem.titleView = searchBarView;

Upvotes: 3

Related Questions