user1282637
user1282637

Reputation: 1867

Hide/Show back button on Navigation Bar when Search Bar opens/closes

So I have a navigation bar that has a "Back" button on it and a UISearchBar to the right of this:

![enter image description here][1]

When the UISearchBar opens/shows, the cancel button hides/shows:

![enter image description here][2]

What I want is when the UISearchBar opens, I want it to basically "cover" the back button. When it closes, I want it so "uncover" the back button. Here is my code so far:

#import "SearchViewController.h"

@interface SearchViewController ()

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

    UISearchBar *searchBar = [UISearchBar new];
    searchBar.showsCancelButton = NO;
    [searchBar sizeToFit];
    searchBar.delegate = self;

    self.navigationItem.titleView = searchBar;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

@end

What I've tried was to do: self.navigationItem.hidesBackButton = NO/YES; in the searchBarTextDidBeginEditing/searchBarTextDidEndEditing but this leaves the spot where the back button was empty:

![enter image description here][3]

Is there a way I can have the search bar extend over the back button? Thanks!

Upvotes: 2

Views: 2738

Answers (1)

Dharmesh Dhorajiya
Dharmesh Dhorajiya

Reputation: 3984

Try to do it by

self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

or

self.navigationItem.backBarButtonItem=nil;

Updated code :

@interface SearchViewController ()
{
    UIBarButtonItem *backButtonItem;
    UIBarButtonItem *negativeSpacer;
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UISearchBar *searchBar = [UISearchBar new];
    searchBar.showsCancelButton = NO;
    [searchBar sizeToFit];
    searchBar.delegate = self;

    self.navigationItem.titleView = searchBar;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];

    self.navigationItem.backBarButtonItem=nil;
    self.navigationItem.hidesBackButton=YES;

    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
    UIImage *backImage = [UIImage imageNamed:@"Back.png"];
    [backButton setImage:backImage  forState:UIControlStateNormal];
    [backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [negativeSpacer setWidth:-15];

    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
    self.navigationItem.leftBarButtonItems = nil;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (IBAction)backButtonPressed:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 4

Related Questions