Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a ContainerView inside a UIViewController created from .xib

Tags:

I have a .xib file and i want to add it a container view (to place inside a ViewController). Unfortunately a container view is only disposable by storyboard. But when i create a .xib file and i search for the container view controller, i don´t found it. Can someone give me a tips how to achieve my task?

like image 740
Fahem Issameddine Avatar asked Sep 13 '15 12:09

Fahem Issameddine


People also ask

How do you add embed segue to a storyboard?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How do you use container view?

Create a single view application in Xcode. Take a label for the top of the view controller and an image view at the bottom. Set constrain and take outlets for them. We will take two different container views from the object library and set them as child view controllers of this “View Controller”.


1 Answers

If you're using a xib instead of a storyboard, you can just add a plain UIView to the xib to act as a container. Then in code, add your childViewController's view as a subview of the container. Here, I've followed the appropriate child view controller methods and added layout constraints to ensure its frame updates with the container's frame:

- (void)viewDidLoad {     [super viewDidLoad];      UIViewController *childViewController = ...; // create your child view controller      [self addChildViewController:childViewController];     [self.containerView addSubview:childViewController.view];     [childViewController didMoveToParentViewController:self];      NSArray *horzConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[childView]|"                                                                    options:0                                                                    metrics:nil                                                                      views:@{@"childView" : childViewController.view}];      NSArray *vertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[childView]|"                                                                    options:0                                                                    metrics:nil                                                                      views:@{@"childView" : childViewController.view}];      [self.view addConstraints:horzConstraints];     [self.view addConstraints:vertConstraints];      childViewController.view.translatesAutoresizingMaskIntoConstraints = NO; } 
like image 179
johnpatrickmorgan Avatar answered Sep 30 '22 06:09

johnpatrickmorgan