Illep
Illep

Reputation: 16841

How to remove default back navigation button

I have added a navigation controller to my login screen. Upon user sign in, the view will get redirected to another viewcontroller called DetailVC.

If the user has been previously logged in the user will automatically navigate to DetailVC. This part works perfectly. However, the default back button in the navigation controller appears on DetailVC. How can i remove it ?

On the DetailVC i will be navigating to other views, and the navigation back button should be displayed from that point onwards. I only want it to disappear on DetailVC. How can i get this done ?

enter image description here

Upvotes: 0

Views: 359

Answers (3)

i_am_jorf
i_am_jorf

Reputation: 54600

An alternative to Rory's answer, but in a similar vein, instead of pushing your new view controller when the login completes, consider instead:

[self.navigationController setViewControllers:@[ myDetailVC ] animated:YES];

This will replace your root view controller (login) with the new one, and thus there will be no back button.

Upvotes: 1

Rory McKinnel
Rory McKinnel

Reputation: 8014

You could adjust the navigation bar, but this seems like working around a problem created rather than avoiding the problem.

It might be more correct to separate your application into two parts: login and logged in.

In your app delegate on load I would create the login controller as a separate item and your DetailVC as a separate item. Put the DetailVC as the root controller of UINavigationController.

Something like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

  // Create login and logged in controllers as properties.
  self.loginController = ... // Use storyboard or however you init this
  self.detailVC = ...        // Use storyboard or however you init this
  self.loggedInController = ... //UINavigationController with root as self.detailVC

  // Check for logged in
  if (self.loggedIn){
    [self switchToLoggedIn];
  }
  else{
    [self switchToLogin];
  }
}

- (void) switchToLogin{
  self.window.rootViewController = self.loginController;
  [self.window makeKeyAndVisible];
}

- (void) switchToLoggedIn{
  self.window.rootViewController = self.loggedInController;
  [self.window makeKeyAndVisible];
}

In your login code you can ask the app delegate instance to switch to the logged in controller once authenticated and on logout switch to the login controller.

As the logged in controller is a UINavigationController it will start as the root properly with no back button.


Another simpler but similar option is to modally present your DetailVC in a UINavigationController from the login. As it is the start of a new stack, it will have no back button. To logout, simply dismiss the controllers navigation controller.

Upvotes: 1

Choppin Broccoli
Choppin Broccoli

Reputation: 3066

This should do the trick:

[self.navigationItem setHidesBackButton:YES];

Or else:

self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

Upvotes: 3

Related Questions