My App's structure likes this:
UISplitViewController:
the master:NavigationController1->UITableViewController
the detail:NavigationController2->UIWebViewController
I want to show the barButtonItem when the view goes on portrait mode on iPad and I know how to realize it in iOS7 by willHideViewController:
-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc{
barButtonItem.title = @"Course";
self.navigationItem.leftBarButtonItem = barButtonItem;
}
-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{
if (barButtonItem == self.navigationItem.leftBarButtonItem) {
self.navigationItem.leftBarButtonItem = nil;
}
}
However, this method is deprecated in iOS 8, and i tried to use:
-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{
if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
self.navigationItem.leftBarButtonItem = svc.displayModeButtonItem;
}else{
self.navigationItem.leftBarButtonItem = nil;
}
}
This method only works when the display mode changes but not when the app first starts with a portrait orientation. So how to show barButtonItem when loading the app first time with a portrait orientation.
You can add the bar button when your view controller shows up:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden)
{
UIBarButtonItem *barButtonItem = self.splitViewController.displayModeButtonItem;
barButtonItem.title = @"Show master";
self.navigationItem.leftBarButtonItem = barButtonItem;
}
}
This will only add the button when the master is currently hidden.