Navigation controller with two views - ios

I have a navigation controller that controls view switch. How can I show a view without removing the current view?

if your new view has a view controller use
[self.navigationController presentModalViewController:newController animated:YES];

This will add a view to the viewcontrollers view.
[parentView addSubview:subView];
This will add the view in variable subView into the view in variable parentView. In your case this would mean parentView is the viewcontroller's view and the subView is the view you want to add.
You can change the position of the subView in the parentView by setting the subview's subview.frame property.
If you are talking about a viewController you want to show take a look at the answer of Andiih.

Related

Manually Trigger `automaticallyAdjustsScrollViewInsets` at a Certain Time

I have a UINavigationController, containing a UIViewController that is parent to two UITableViewController controllers.
When the user taps on a segmented control in the UIToolbar of the navigation controller, the current child table controller is swapped out with the new one. This includes removing the old controller from the parent hierarchy and removing its view as a subview of the parent view controller.
The first view controller that is displayed when the navigation view controller first presents it has its contentInset correctly configured by automaticallyAdjustsScrollViewInsets, however, when I pull that one out and insert the view from the second table view controller, that does not.
Furthermore, if I rotate the device (Which shrinks the UINavigationBar) and then swap back to the first view controller, its contentInset is now incorrect and it doesn't scroll properly. The second controller, however, does have its contentInset property properly set as a result of the device rotation.
Is there a way to manually force a UIViewController to redo its automaticallyAdjustsScrollViewInsets operation when I need it?
It's not an absolutely amazing one, but I found a solution that works.
Inserting a new child view controller isn't enough to trigger UINavigationController to automatically work out the appropriate contentInset values for any scroll views in the new child. BUT! You can force it to perform that calculation by doing something that would have required it anyway. For example, hiding and showing the navigation bar or toolbar.
- (void)insertViewController:(UIViewController *)viewController
{
// Add the view to our view
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
// Add the new controller as a child
[self addChildViewController:viewController];
[viewController didMoveToParentViewController:self];
// Show and hide the toolbar to force the content inset calculation
self.navigationController.toolbarHidden = YES;
self.navigationController.toolbarHidden = NO;
}
I've tested it, and there appear to be no visual glitches by rapidly hiding either the navigation bar or toolbar, so this solution seems to be acceptable.

How to set a view over a child view controller's view?

I am wondering if it is possible to set a view controller's view over one of its child view controller's view ?
Let's explain that with an example :
Let's say I have two UIViewControllers : parentVCand childVC. childVC is a child of parentVC.
Now, I have a UIButton ( called button ) which is a subview of parentVC.view.
I set childVC from parentVC like that :
self.addChildViewController(childVC)
childVC.didMoveToParentViewController(self)
childVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height
self.view.addSubview(childVC.view)
Now, I want button to show up even if childVC.view covers parentVC.view.
Do you guys think there's a way to do that ? I tried to play with button.layer.zPosition but I couldn't succeed.
Yes, you can, you just need to add the button to the view hierarchy or move the button up in the hierarchy after you add the child view controller.
yes you can do that first of all when you load a childview on a viewcontroller make a view like. There is a container on top covering almost 90% screen and at bottom there is a button,So when you load the view on container and sub view show's you on top if you tap a button then add a subview and bring that subview to front.

Adding a child viewcontroller

I have 2 views, ParentViewController and ChildViewController; I want to nest ChildViewController inside ParentViewController. I have designed ParentViewController and ChildViewController in Storyboard. ParentViewController.m contains the logic for the parent and ChildViewController.m contains the logic for the child. In ParentViewController.m I add the child like so:
ChildViewController *childVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildSBI"];
[self addChildViewController:childVC];
My Question: How can I position childVC within ParentViewController (i.e. set it's origin).
It seems like I want to do something like the following:
Alloc init child
Call (void)addSubview:(UIView *)view on the view of the child
How can I position childVC within ParentViewController (i.e. set it's origin).
Remember that a view controller doesn't have an origin -- only its view does. To make a view controller's content appear on screen, you need a view to draw it in. You can have one view controller's view appear in another view controller's view hierarchy, and it's quite easy to do with storyboards.
To set up a parent/child relationship between view controllers in a storyboard, do the following:
Add a container view to the parent view controller's view hierarchy.
Drag an "embed" segue from that container view to the child view controller.
You can use the usual -prepareForSegue:sender: method to let the parent controller pass data to the child.
You can also set things up programmatically, if you prefer, and the code you've got is a good start:
ChildViewController *childVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildSBI"];
[self addChildViewController:childVC];
After that, your parent view controller should set the child's view's position and add it to the parent's view, like this:
UIView *childView = childVC.view;
childView.frame = CGRectMake(150, 300, 100, 100); //use whatever coordinates you want
[self.view addSubview:childView];
There's a lot more detail in Creating Custom Container Controllers, but the basic answer to your question is that the container controller should set the size and position of the contained controller's view.

Adjusting a Child ViewController inside another viewController

I am trying out an application using a child ViewController inside another viewController.
I have a VC and I am instantiating another VC with its own xib inside the outer VC.
I am adding it as a child using the new iOS 5 method addChildViewController and also I have added its view as a subView.
But how do I control its position and size inside the parent view controller ?
should I modify the frame of the child controller's view ?
or I have to adjust the freeform view in the xib itself ?
Also in my current implementation, the child view starts behind the status bar of the parent viewcontroller's view.
Any idea on how to systematically implemement something like this ?
#define SUBVIEWS_FRAME CGRectMake(0,20,100,100) // whatever frame you need
- (void)addChildViewController:(UIViewController *)childController{
[childController.view setFrame:SUBVIEWS_FRAME];
[super addChildViewController:childController];
}
Simply just add a view in parentViewController #synthesize that view like childView. now add your childViewController as a subView in childView of parentViewController. it will simple to adjust using parentViewController View on nib file. If you have a large childViewController then please use UIScrollView instad of UIView

Pinned subview of a popover controller having a tableview inside

I am trying to add a hovering subview to a UIPopoverController. I have a table view controller as a content view controller inside the popover. I tried to add it as a normal subview:
UIPopoverController* popoverController = [[UIPopoverController alloc] initWithContentViewController:myTableViewController];
UIView* mySubview = ...
[popoverController.contentViewController.view addSubview:mySubView];
It is displayed correctly but unfortunately scrolls up and down with the table view. I would like to have its position fixed.
I also tried to update the position of the subview by the y offset of the scrollview in the scrollViewDidScroll: method of the table view controller, but would like to avoid this solution if possible.
You have to make the content controller a UIViewController subclass rather than a UITableViewController subclass.

Resources