Splitview side bar hiding

So you have this splitview but you don’t want to show it all the time in landscape, but Apple never explained how to do that. Let’s make up on that:

First add some button logic in the app delegate:

– (IBAction)toggleSplitView:(id)sender {

    if ( [window.subviews containsObject:splitViewController.view] ) {

        [splitViewController.view removeFromSuperview];

        splitViewController.viewControllers = [NSArray arrayWithObjects:leftViewController, leftViewController, nil]; 

        [window addSubview:rightViewController.view];

        [(UIBarButtonItem *)sender setTitle:@”Show Sidebar”];

    } else {

        [rightViewController.view removeFromSuperview];

        [rightViewController.view setTransform:CGAffineTransformIdentity];

        [navigationController popToRootViewControllerAnimated:NO]; // make sure we are on top

        [navigationController setViewControllers:[NSArray arrayWithObject: leftViewController]];

        splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightViewController, nil]; 

        [window addSubview:splitViewController.view];

        [rightViewController.view sizeToFit];

        [(UIBarButtonItem *)sender setTitle:@”Hide Sidebar”];

    }

}

 

In the controller that handles your right view (detail) add a item:

    UIBarItem *toggleSidebar;

@property (nonatomic, retain) IBOutlet UIBarItem *toggleSidebar;

Insert the control when the controller is triggered during hide and show:

– (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

    NSMutableArray *items = [[toolbar items] mutableCopy];

    [items removeObjectAtIndex:0];

    [items insertObject:toggleSidebar atIndex:0]; // add additional control for hiding

    [toolbar setItems:items animated:YES];

    [items release];

    self.popoverController = nil;

}

Add a button in the load view that triggers the AppDelegate:

    toggleSidebar = [[UIBarButtonItem alloc] initWithTitle:@”Hide sidebar”

                                                     style:UIBarButtonItemStyleBordered 

                                                    target: [self appDelegate]

                                                    action:@selector(toggleSplitView:)];

And don’t forget to put in in the toolbar 🙂

    [items insertObject:toggleSidebar atIndex:0];

// set the bar

    [toolbar setItems:items animated:YES];

Oh and hide it in the willHideController

Dealoc it when done:

– (void)dealloc {

    [popoverController release];

    [toolbar release];

    [toggleSidebar release];

    [super dealloc];

}

 

Advertisements
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories
%d bloggers like this: