Swapping child views in a container view
When you have child views that have their own view controllers, you should be following the custom container controller pattern. See Creating Custom Container View Controllers for more information.
Assuming you've followed the custom container pattern, when you want to change the child view controller (and its associated view) for the "content view", you would do that programmatically with something like:
UIViewController *newController = ... // instantiate new controller however you want
UIViewController *oldController = ... // grab the existing controller for the current "content view"; perhaps you maintain this in your own ivar; perhaps you just look this up in self.childViewControllers
newController.view.frame = oldController.view.frame;
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController]; // incidentally, this does the `willMoveToParentViewController` for the new controller for you
[self transitionFromViewController:oldController
toViewController:newController
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
// no further animations required
}
completion:^(BOOL finished) {
[oldController removeFromParentViewController]; // incidentally, this does the `didMoveToParentViewController` for the old controller for you
[newController didMoveToParentViewController:self];
}];
When you do it this way, there's no need for any delegate-protocol interface with the content view's controller (other than what iOS already provides with the Managing Child View Controllers in a Custom Container methods).
By the way, this assumes that the initial child controller associated with that content view was added like so:
UIViewController *childController = ... // instantiate the content view's controller any way you want
[self addChildViewController:childController];
childController.view.frame = ... // set the frame any way you want
[self.view addSubview:childController.view];
[childController didMoveToParentViewController:self];
If you want a child controller to tell the parent to change the controller associated with the content view, you would:
Define a protocol for this:
@protocol ContainerParent <NSObject>
- (void)changeContentTo:(UIViewController *)controller;
@endDefine the parent controller to conform to this protocol, e.g.:
#import <UIKit/UIKit.h>
#import "ContainerParent.h"
@interface ViewController : UIViewController <ContainerParent>
@endImplement the
changeContentTo
method in the parent controller (much as outlined above):- (void)changeContentTo:(UIViewController *)controller
{
UIViewController *newController = controller;
UIViewController *oldController = ... // grab reference of current child from `self.childViewControllers or from some property where you stored it
newController.view.frame = oldController.view.frame;
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
[self transitionFromViewController:oldController
toViewController:newController
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
// no further animations required
}
completion:^(BOOL finished) {
[oldController removeFromParentViewController];
[newController didMoveToParentViewController:self];
}];
}And the child controllers can now use this protocol in reference to the
self.parentViewController
property that iOS provides for you:- (IBAction)didTouchUpInsideButton:(id)sender
{
id <ContainerParent> parentViewController = (id)self.parentViewController;
NSAssert([parentViewController respondsToSelector:@selector(changeContentTo:)], @"Parent must conform to ContainerParent protocol");
UIViewController *newChild = ... // instantiate the new child controller any way you want
[parentViewController changeContentTo:newChild];
}
Swapping VC's in a Container View - childVC does not fit Container
OK crisis over. Turns out I was designing in storyboard for a 12 inch iPad and running as a 9.7 inch iPad. Several views had fixed value heights/widths - hence the wonky appearance when running the app.
Swap view controllers in a container view
If you have not started your work yet I would suggest using UITabViewController
in place of container view in UIViewController
. Using UITabViewController
has one advantage over using container view that you dont have to manage Child View Controller your UITabViewController
manages it all. As apple documentation says UITabBarController
is a specialized view controller that manages a radio-style selection interface.
But if you have already started working on container view, you may want it working.I too learned regarding container view from the same link which @iOSGeek has shared. Every things is clearly explained. You can follow the same link and you are good to go.
Hope it helps. Happy Coding!!
Container View in Cocoa, Swap Contents at Runtime and Preserve Size
It turns out all I needed to do is add this line before setting up my constraints:
newView.translatesAutoresizingMaskIntoConstraints = false
It isn't clear to me why I should need this, given that the child view I want to add belongs to a view controller created from a storyboard (and those are supposed to already not translate autoresizing mask into constraints...?); this code should only be needed for views created programmatically...
Switching containerView content from within viewController
While click on ButtonA. Post a notification to mainView. There remove viewController A from Container and add View Controller B .
how to load child view controllers or container views - Swift
As i understand there are 2 container views and you want to see the blue view when the segmented is first and you want to see the green view when the segmented is second if it is true the solution is like this;
You should init the uivew(hold on the ctrl and drag it to viewcontroller for both blue and green container view)(you need to add 2 different container view) and write
if segmented.selectedIndex == 0 {
greenView.isHidden = true
blueView.isHidden = false
} else if segmented.selectedIndex == 1 {
greenView.isHidden = false
blueView.isHidden = true
}
Related Topics
How to Check If iOS App Is in Background
Editing Screenshots in Itunes Connect After iOS App Was Approved
Get iOS Itunes App Store Id of an App Itself
Differencebetween a Development Provisioning Profile and an Ad Hoc Provisioning Profile
Nsurlsession "Http Load Failed Kcfstreamerrordomainssl, -9813 ; Self Signing Certificate
How to Check If Device Can Make a Phone Call (iOS 8)
Mapkit iOS 9 Detailcalloutaccessoryview Usage
What Does It Mean to "Weak-Link" a Framework
Swift If Statement - Multiple Conditions Separated by Commas
How to Delete App Build in New Itunes Connect Site
Swapping Child Views in a Container View
Find Facebook Sdk Version on iOS
Swift Regex: Does a String Match a Pattern
Autolayout Multiline Uilabel Cutting Off Some Text
Swift "Bridging-Header.H" File Not Allowing Me to Instantiate Objective-C Classes in .Swift Files