Cancel button is not shown in UISearchBar

Sheik_101 picture Sheik_101 · May 27, 2015 · Viewed 12.3k times · Source

I have UICollectionView. On clicking search button in UINavigationBar, I am adding the UISearchController's searchbar as titleview for UINavigationItem. For iPhone it is working properly. For iPad the cancel button is not shown. The Searchbar alone takes the entire width.

enter image description here

Can anyone help me out on this?. Thanks in advance.

Answer

Nikita Khandelwal picture Nikita Khandelwal · May 27, 2015

iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar in another view like this.

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;