How to hide MasterViewController in UISplitViewController for iPad - ios

I am working with UISplitViewController,where i need to hide
MasterViewController for 1st DetailViewController. Means,when app
launches,the MasterViewController must be hidden without clicking on
any UIButton. I tried using UISplitViewController inside
UIViewControllers.But nothing happened. After that I tried to develop overall app within UISplitViewController. Here is the code I tried to hide MasterViewController
masterViewController = [splitViewController.viewControllers objectAtIndex:0];
detailVC = [splitViewController.viewControllers objectAtIndex:1];
[master.view setFrame:CGRectMake(0, 0, 0, 0)];
detail.view.frame = splitViewController.view.bounds;
I have spent my 1 week doing this,but nothing worked.Please provide
solution.It would be great pleasure.
UPDATE:
Ok done with hiding master view controller.But when i set delegate to
detail view controller,it hides master view controller for all view
controllers. Here is my appDelegate's code where I am adding
UISplitViewController.
self.splitViewController =[[UISplitViewController alloc]init];
MasterTableViewController *masterViewController=[[MasterTableViewController alloc]initWithNibName:#"MasterTableViewController" bundle:nil];
UINavigationController *masterNavigate=[[UINavigationController alloc]initWithRootViewController:masterViewController];
DetailViewController *detailVC=[[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *detailNavigate=[[UINavigationController alloc]initWithRootViewController:detailVC];
self.splitViewController.viewControllers=[NSArray arrayWithObjects:masterNavigate,detailNavigate, nil];
self.splitViewController.delegate=detailVC;
self.window.rootViewController=self.splitViewController;
Here,I set delegate of splitViewController to detailVC.In
detailViewController with Nex Mishra's code I am hiding master view
controller and when I navigate to other views from detailVC,it hides
the master view controller. I know it would be irritating,But it would
be helpful.

You could use this delegate method to hide the master VC.
- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
//Add your own logic to when you want the master VC hidden.
return YES;
}

- (BOOL)splitViewController:(UISplitViewController*)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
// this commant is used for showing or hiding the split view controller
svc.presentsWithGesture = TRUE;
return YES;
}
// dont forget to assign the delegate like this in viewWillAppear
(self.splitViewController.delegate = self;)

Related

Which NavigationController Method runs on every view reload (TabBarItem)

I have a NavigationController then a TabBarController which has Four Tabs.
I wanted to display Different titles on TopBar when a Different Tab is selected.
One way was to Embed each TabBarItem View into Navigation Controller but for some reason this doesn't seems the correct way, i wanted to apply this via code.
I managed accomplish this by using this code: (Products_ViewController.m custom class)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
But the problem is now when a tab is clicked First time, it changes the title but then it doesn't. I then applied the same code on -(void)viewDidAppear{} but still the same result.
How can i manage to display navigation top bar title (or run the above code) whenever the tab bar item is clicked or the view is shown?
Thanks!
You could implement the UITabBarControllerDelegate in the Products_ViewController.m class and execute your code in the tabBarController:didSelectViewController: method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
In the viewDidLoad method you have to set the delegate to self.

UITabBar disappears after pushed to new view controller

I have an UITabBarController that has 3 buttons. The second button points to ViewController1 which is connected to another view called ViewController2. After I tap a button in ViewController2 I programmatically present ViewController1 again, that works perfect except one thing. After I "arrived" to ViewController1 the tab bar disappears.
I'm using this method to navigate back to ViewController1. (exactly I navigate to its navigation controller, but already tried with the view)
- (void)presentViewControllerAnimated:(BOOL)animated {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard" bundle:nil];
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
}
I call here the first method
- (void)didTapButton:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
[self presentViewControllerAnimated:YES];
}
This method hides the tab bar in the ViewController2, I already tried without it, therefore there is no problem with it.
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}
I can't figure out why this thing happens, I think it's a fair solution, that worked well for a several times when I needed to present views. I've read it can happen with segues, but I'm doing it with code without segues.
Actually your code works right. There should not be tab bar when you present FirstViewController from SecondViewController. Because when you call instantiateViewControllerWithIdentifier its basically creates a new instance of that view controller, and of course, there is no tab bar.
The right way to go back to your first view controller is to pop SecondViewController (or dismiss it, if it presented modally). So your final code should be like this
- (void)didTapButton:(id)sender {
// If this view controller (i.e. SecondViewController) was pushed, like in your case, then
[self.navigationController popViewControllerAnimated:YES];
// If this view controller was presented modally, then
// [self dismissViewControllerAnimated:YES completion:nil];
}
And of course, your view controller hierarchy in storyboard must be like this:
-- UINavigationController -> FirstViewController -> SecondViewController
|
->UITabBarController____|
-...
-...
I've tried the same and got the same result.
My solution was simple, on the push do this :
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
firstViewNavigationController.hidesBottomBarWhenPushed = true; // Insert this and set it to what you want to do
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
and then remove your
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}

How to hide the master view if navigate to new screen from detail view controller

I have a split view controller with master view and detail view. Master View is fixed in the delegate function:
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:
(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return enableMaster;
}
When I click the button on detail view it navigates to a new screen. But on the new screen it still displays the master view.
Code for navigation:
NextViewController *viewController = [...]; \\initialised here;
[self.navigationController pushViewController:viewController animated:YES];
My question:
1. When I navigate to new screen from detail view controller, it should display next screen as a full screen. How to do that?
Note: I don't want to use "presentViewController" to display the next screen because of two reasons.
1. I want to come to previous screen using navigation stack
2. All the screens are also based on Tab bar controller, and if I use "presentViewController" then tab bar controller not longer visible for new screen.
After reading this post:
How to hide & unhide Master View Controller in SplitView Controller
Solution:
finally my problem has solved:
// In split delegate
-(void)hideMaster:(BOOL)hideState
{
_masterIsHidden = hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication
sharedApplication].statusBarOrientation duration:0];
}
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:
(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return self.masterIsHidden;
}
//Call On button:
-(void) btn_click
{
[self hideMaster: TRUE];
}
What you need to do is embed your UISplitViewController in a navigation controller itself. You'll want to hide this navigation controller's navigation bar so you don't end up with two. When you go to present your detail view, push it on the split view's navigation controller. It would look something like this:
NextViewController *viewController = [...]; \\initialised here;
UISplitViewController *splitVC = self.parentViewController.parentViewController;
[splitVC.navigationController pushViewController:viewController animated:YES];
Note that this is somewhat fragile; if the view controller hierarchy changes, the parent of the parent trick may break. It could be wise to keep a reference to your top level navigation controller on your app delegate (it should be the root view controller of your key window) and access it this way to future-proof your solution.

iOS SplitView Controller, Programatically Change View With Multiple Storyboards

Hopefully someone can help.
I've got an app that uses the UISplitViewController however I've now had to start using multiple storyboards because i've got a large amount of views and Xcode was starting to run really slow. I've moved moved the UIViewControllers to multiple storyboards.
The Master View is built from static cells, so when the user selects the cell I normally change the view by pushing a segue.
I'm now wondering how to programmatically change the detail view of a UISplitViewController?
Thanks
Subclass UISplitViewController and set your root splitViewController to that class. Then add this method to your UISplitViewController subclass:
-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called
if (title) {
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:detailController animated:YES];
detailController.title = title;
NSArray *viewControllers=#[self.mainController.viewControllers[0],navController];
self.mainController.viewControllers = viewControllers;
} else {
NSArray *viewControllers=#[self.mainController.viewControllers[0],detailController];
self.mainController.viewControllers = viewControllers;
}
}
To call this method do something like this from the master view controller:
FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;
CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:#"Current Event"];
A lot of my projects require the splitviewcontroller to always show the master view so I use this subclass to keep the master view from hiding on portrait rotation.

How to structure ViewControllers using Storyboard for SplitView iPad app?

I'm trying to build the interface for an app that looks like this:
I want to use the Storyboard to create the views, but I'm having trouble figuring out how now. I've dropped a SplitViewController that is hooked up to 2 navigation controllers: MasterNavigationController, and DetailNavigationController (Master is the left side menu and Detail the right side).
I was starting to build all my ViewControllers in storyboard. Then build NSArrays to hold the various ViewController stacks needed for each of the Master's menu items. So when a user taps on a menu item, I'd load the corresponding ViewController stack into the DetailNavigationController using this method:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
But now I'm thinking, I should create a separate DetailNavigationController for each of the menu items. For example:
RecentOrdersNavigationController
CustomersNavigationController
ItemsNavigationController
...
Then when a user taps a menu item, the entire DetailNavigationController changes to the appropriate one.
How should I be structuring the the interface while using storyboard?
Use a separate viewController for each detail view. You want your code to be separate since it can get confusing fast with all the different functionality for each view. From there, you can easily swap out the the detail.
Subclass UISplitViewController and set your root splitViewController to that class. Then add this method to your UISplitViewController subclass:
-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called
if (title) {
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:detailController animated:YES];
detailController.title = title;
NSArray *viewControllers=#[self.mainController.viewControllers[0],navController];
self.mainController.viewControllers = viewControllers;
} else {
NSArray *viewControllers=#[self.mainController.viewControllers[0],detailController];
self.mainController.viewControllers = viewControllers;
}
}
To call this method do something like this from the master view controller in the tableView:didSelectRowAtIndexPath: method
FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;
CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:#"Current Event"];
If you wish to keep the master view visible in portrait rotation, add this method to the SplitViewController subclass:
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return NO;
}
A lot of my projects require the splitviewcontroller to always show the master view so I use this subclass to keep the master view from hiding on portrait rotation.

Resources