Is there is any way to hide the master view in a splitviewcontroller programmatically. In my application the first screen will be of a splitviewcontroller, i don\'t need any
This works:
Attach the "hide" method to your button, for example:
UIBarButtonItem *hideButton = [[UIBarButtonItem alloc] initWithTitle:@"hide"
style: UIBarButtonItemStylePlain
target: self
action: @selector(hide:)
];
[self.mainView.navigationItem setLeftBarButtonItem:hideButton];
In this code, the "self.mainView" is the view controller in the navigation controller in the second view of the splitview - just as a reference.
The hide method looks like so.
-(void)hide:(id)sender
{
UIViewController *masterController = [self.viewControllers objectAtIndex:0];
CGRect selfFrame = self.view.frame;
CGFloat aWidth = masterController.view.frame.size.width;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
selfFrame.size.height += aWidth;
selfFrame.origin.y -= aWidth;
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
selfFrame.size.height += aWidth;
}
[self.view setFrame:selfFrame];
[UIView commitAnimations];
}
This is the starting point, obviously more logic needs to be done to take care of rotation, showing it again, etc...
I hope this helps.
Tested with iOS5 and Xcode 4.3
While it will not have nice transitions (sorry), you could do this by setting the root view to the detail view controller's view, and then swap views with the UISplitView and move the detail view to the UISplitView. (Actually you might be able to animate the view swap (push/flip/etc.) but it is a bad idea to change anything during view change animations, and moving the detail view to inside the UISplitView might qualify.)
The BarButtonItem provided by the SplitViewController is the key to programmatically hiding the Master View Controller.
This code is DANGEROUS! but elegant :)
import the objective c message library
#import <objc/message.h>
Next, get a handle to the UIBarButtonItem provided by the SplitViewController
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = @"Master";
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
//Obtain handle to BarButtonItem
[self setMasterButtonItem:barButtonItem];
}
Then when the event is fired which should trigger the auto-dismissal of the master view controller i.e.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
You can do this
objc_msgSend(self.masterButtonItem.target, self.masterButtonItem.action);