Load UIViewController inside a Container View using StoryBoard
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
[news didMoveToParentViewController:self];
}
Swift
override func viewDidLoad() {
super.viewDidLoad()
let news = self.storyboard?.instantiateViewControllerWithIdentifier("NewsViewControllerID") as! NewsViewController
news.view.frame = newsSection.bounds
newsSection.addSubview(news.view)
addChildViewController(news)
news.didMoveToParentViewController(self)
}
Swift >= 3:
override func viewDidLoad() {
super.viewDidLoad()
let news = self.storyboard?.instantiateViewController(withIdentifier: "NewsViewControllerID") as! NewsViewController
news.view.frame = newsSection.bounds
newsSection.addSubview(news.view)
addChildViewController(news)
news.didMove(toParentViewController: self)
}
Loading a ViewController inside a Container View
You need to tell your BannerContainer view controller that it has a new child controller, and to tell the Child that it has a parent VC. This is described in the Apple Docs here. Like this:
[self addChildViewController:vc];
vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
[self.container addSubview:vc.view];
[vc didMoveToParentViewController:self];
Or in Swift:
self.addChildViewController(vc)
vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
self.container.addSubview(vc.view)
vc.didMoveToParentViewController(self)
This ensures that various layout and touch methods are passed through to the child VC; I suspect the layout problems you have may be due to those methods not currently being called.
How to load a view controller into a container view?
Hello You can use this code.
let view2 = self.storyboard?.instantiateViewController(withIdentifier: "View2")
addChildViewController(view2!)
view2?.view.frame = CGRect.init(x: 0, y: 0, width: self.viewContainer.frame.size.width, height:
self.viewContainer.frame.size.height)
self.viewContainer.addSubview((view2?.view)!)
view2?.didMove(toParentViewController: self)
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
}
Load ViewController into ContainerView prior to showing so no transition is visible
TopNav.pushViewController(toVC, animated: true)
and it works - but only after TopViewController becomes visible.
Because you asked for animation. Change animated: true
to animated: false
If that’s what you want.
The preceding is right, but there were a lot of other issues with your project.
Most important, your container view has no constraints. This causes it to end up the wrong size. Give it constraints in the storyboard.
Second, do what I said above: push toVC
with false
animation.
Third, you need to preload your top view controller's view, like this:
@IBAction func Item1_Tap(_ sender: UIButton) {
let toVC = self.storyboard?.instantiateViewController(
withIdentifier: "TopViewController") as! TopViewController
toVC.loadViewIfNeeded() // add this line
// then delete all the other useless stuff
Result:
Loading a UIViewController into a Container View programmatically
Make sure this @IBOutlet var customContainer: UIView!
is linked to the containerview and not to the vc's main view in storyboard
From within a view controller in a container view, how do you access the view controller containing the container?
You can use the prepareForSegue
method in Vc1 as an embed segue occurs when the ContainerViewController is made a child. you can pass self as an obj or store a reference to the child for later use.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * segueName = segue.identifier;
if ([segueName isEqualToString: @"embedseg"]) {
UINavigationController * navViewController = (UINavigationController *) [segue destinationViewController];
Vc2 *detail=[navViewController viewControllers][0];
Vc2.parentController=self;
}
}
Edit: minor code fix
Embed view controller inside container view based on selection from tableview
First disable the segue form your container view to a DestinationViewController
in your storyboard.
Now load your viewController object based on your previous tableViewController selection.
//this controller will be change on tableView selection make your own logic here.
let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
addChildViewController(controller)
//Set this value false if you want to set Autolayout programmatically or set it true if you want to handle it with `frame`
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
Related Topics
Rotating a View in Layoutsubviews
How to Set Image in Tabbar Not Tint Color in iOS
How to Get Navigation Based Template Functionality in Swift Programming
Swift Uicolor Initializer - Compiler Error Only When Targeting Iphone5S
Catch an Exception for Invalid User Input in Swift
How to Make iPhone Vibrate Using Swift
How to Display the Default iOS 6 Share Action Sheet with Available Share Options
Ask for User Permission to Receive Uilocalnotifications in iOS 8
Xcode 8.3/Xcode 9.0 Refresh Provisioning Profile Devices
Is There a Public Way to Force Mpnowplayinginfocenter to Show Podcast Controls
Getting Back a Date from a String
Xcode Process Launch Failed: Security
Updating to Latest Version of Cocoapods