Search code examples
iosxcodeuisplitviewcontrolleruibarbuttonitemuitoolbar

UISplitViewController with UIToolbar not showing UIBarButtonItem in landscape mode


I am new to SplitViewController and I am running into the following issue. Here is the setup:

In IB, I only have a VC linked to the SVC as the detail VC (This compares to the standard Master Detail project template which has a Nav Controller as the starting point for the detail Hierarchy). I am doing this to try to maximize image space in the detail VC.

I then add a UIToolbar (called imageTable) to the detail VC which I connect with an IBoutlet to the detail VC class.

I am using the detail VC as the SVC delegate.

I then adopt the follow SVC delegate 3 methods in the detail VC:

#pragma mark - SplitViewController Delegates
-(BOOL) splitViewController:(UISplitViewController *)svc
   shouldHideViewController:(UIViewController *)vc
             inOrientation:(UIInterfaceOrientation)orientation
{
  return UIInterfaceOrientationIsPortrait(orientation); 
}



-(void) splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
         withBarButtonItem:(UIBarButtonItem *)barButtonItem
      forPopoverController:(UIPopoverController *)pc
{    
  barButtonItem.title=@"ImageList";
  NSMutableArray *toolbarItems=[self.imageTable.items mutableCopy];
  [toolbarItems insertObject:barButtonItem atIndex:0];
  self.imageTable.items=toolbarItems;
}



-(void) splitViewController:(UISplitViewController *)svc
     willShowViewController:(UIViewController *)aViewController
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
  NSMutableArray *toolbarItems=[self.imageTable.items mutableCopy];
  [toolbarItems removeObjectAtIndex:0];
  self.imageTable.items=toolbarItems;
}

Because I am not using Nav Controller as the starting point in the VC, I am running into the following:

  • UIToolBar automatically disappears when I turn back to landscape. Is it inherent in the SVC class that since the UIBarbuttonItem representing the VC to reappear is no longer needed in landscape, so is the UIToolbar I created (emphasizing 'I')...

  • Is there any way to add or enable a title to the UIToolbar or is the only way using a label.

Thanks


Solution

  • I have been pushing a bit forward in CS193P class and I gather the simple answer is:

    If you are ever in need of a UIToolbar (to add Buttons or a title), just embed the Table View Controller or any view Controller you are working on, in a Navigation controller - this way you get a free UIToolbar (with easy access to title, etc..)

    Do that even if you have no need to push/pop or segue.

    KMB