Prateek Chaubey
Prateek Chaubey

Reputation: 645

How to remove a button from navigation bar?

I have been working on ipad application. In this application I have several views. here is the flow

Welcome Screen > Home Screen > rest of the sreens

I have applied a home icon (button) on navigation bar of all screens. Pressing home icon on any screen takes the user to home screen. I wrote the following code in the viewDidLoad of Home class

//**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];

homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

This button is functional. goHome is the name of the method applied in the @selector. I want to remove this button from home screen and keep it on rest of the screens. I have applied several things but i have no idea how to do it. This seems very simple but still I am not getting it. Please guide..

Regards PC

Upvotes: 0

Views: 1366

Answers (2)

0x8badf00d
0x8badf00d

Reputation: 6401

In your "Home Screen" viewDidAppear method do this:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
     if(view.tag == 10)
     {
        view.hidden = YES;
     }
}

In your other View controller where you created the button set tag for home button to 10.

/**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homebutton setTag:10]; // Set tag to 10 or any value
homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

And in you viewDidAppear: of this other view Controller:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
    if(view.tag == 10)
    {
      view.hidden = NO;
    }
}

Upvotes: 3

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

Try this:

self.navigationItem.backBarButtonItem = nil;

Upvotes: 1

Related Questions