Reputation: 30078
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?
Thanks.
Upvotes: 3
Views: 1442
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