iOS7 when UIsearchbar added in UINavigationBar not showing cancel button

前端 未结 7 883
误落风尘
误落风尘 2020-12-03 01:33

I add UISearchBar above UINavigationBar and set UIsearchbar showsCancelButton YES, work fine in iOS6 but in iOS7 not showing cancel button. I used below code snippet

相关标签:
7条回答
  • 2020-12-03 01:57

    I had similar problem, on iPhone search bar with cancel button show well, but on iPad the cancel button wasn't shown. Wrapping the UIsearchBar to UIView like @Rodskjegg throw style issue. On iPad UIsearchBar setting it as the titleView of a navigationItem and add UIBarButtonItem to setRighttBarButtonItem as UIBarButtonSystemItemCancel.

        [self.navigationItem setLeftBarButtonItem:Nil animated:YES];
        self.navigationItem.titleView = self.searchBar;
    
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
        {
            UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(searchBarCancelButtonClicked:)];
    
            [self.navigationItem setRightBarButtonItem: cancelButton animated:YES];
        }
        else {
            [self.navigationItem setRightBarButtonItem: nil animated:YES];
        }
    
    0 讨论(0)
  • 2020-12-03 01:58

    Yes In iOS 7 button is located on the screen, but it's title could be invisible My solution was to set Search Style to "Minimal" and choose bar tint colour for "Cancel" text colour in IB

    enter image description here

    And the Result in a simulator :

    enter image description here

    0 讨论(0)
  • 2020-12-03 01:59

    Implement the search bar delegate and use this:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        searchBar.showsCancelButton = YES;
    }
    
    0 讨论(0)
  • 2020-12-03 02:03

    I had the same problem, on iPhone the search cancel was shown well, but on iPad it didn't.

    The workaround of wrapping the UISearchBar in another UIView didn't work well for me since it had different appearance and wrong width on rotation.

    My solution is a simple one - use search WITHOUT cancel, and add cancel as UIBarButtonItem.

    0 讨论(0)
  • 2020-12-03 02:06

    Since iOS 7 you can just set the property displaysSearchBarInNavigationBar to YES on the UISearchDisplayController to automatically get a UISearchbar in the NavigationBar.

    0 讨论(0)
  • 2020-12-03 02:13

    I ran into the same problem, here's my solution, hope this helps.

    Some further explanation: I found out that sending setShowsCancelButton:animated: to the searchBar, it just works like magic. And the cleanest way to add a searchBar to navigation bar is self.navigationItem.titleView = self.searchBar; The appropriate timing for calling setShowsCancelButton:animated: is in searchBarTextDidBeginEditing: and searchBarTextDidEndEditing: delegate methods, so remember to set self to be the delegate of searchBar.

    - (void)viewDidLoad
    {
        self.navigationItem.titleView = self.searchBar;
    }
    
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        [searchBar setShowsCancelButton:YES animated:YES]; 
    }
    
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
        [searchBar setShowsCancelButton:NO animated:YES];
    }
    
    0 讨论(0)
提交回复
热议问题