Search code examples
iosobjective-cuisearchbaruisearchcontroller

Cancel button is not shown in UISearchBar


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.


Solution

  • 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;