iOS - Presenting a UIViewController inside anther

2019-08-09 01:02发布

I dont have a very good understanding on the topic, so please bear with me

I am trying to display a UIViewController inside another UIViewController. I feel like there aren't enough examples out there. I have read the documentation but being fairly new to iOS I am kind of lost.

Here is my problem: I can get the child UIViewController to appear inside the parent UIViewController but the child view is not displaying right.

Sorry, I cant post pictures yet.
Here is my child - UIViewController.
Here is my parent - UIViewController. (The blue area is where the child view is added)

Here are the results:
Blue UIView in place - http://i.imgur.com/4vny8EZ.png

If i move the blue UIView over to the right here is what happens
Blue UiView moved over to the right - http://i.imgur.com/XCOBwr6.png

I have tried a lot of different things but my lack of knowledge on the subject is really making it difficult to find out what is wrong.

Here is how I am loading my child UiViewController in my parent UIViewControllers class:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add Calculations View Controller
    CalculationViewController *calculationController = [self.storyboard instantiateViewControllerWithIdentifier:@"Calculate"];
    [self addChildViewController:calculationController];
    calculationController.view.frame = self.calcualtionView.bounds;

    [self.calcualtionView addSubview:calculationController.view];

}

So, my question is why is it displaying the child view in a weird fashion?
Thank you!

Also, sorry about the links I could only post two.

2条回答
疯言疯语
2楼-- · 2019-08-09 01:34

Move the code to viewDidAppear. If calculationView is an IBOutlet to a view, its bounds will be zero in viewDidLoad. The frames of views are not set until viewDidAppear.

查看更多
成全新的幸福
3楼-- · 2019-08-09 01:49

Quick answer: Views sizes aren't determined until viewWillAppear. Set the frame there and it will work.

Longer answer:

1) If you are using iOS6, I would encourage you to use NSLayoutConstraints to deal with view size and positioning rather than manually setting view frames. It takes a bit to really wrap your head around constaints, but once you do they are much more powerful and flexible than setting frames and you won't have to worry about when or how your IBOutlets are created

2) You are not adding your child view controller correctly. You must call didMoveToParentViewController: after the view is added. With the missing line of code as well as using contraints rather than frames your code looks like this:

CalculationViewController *calculationController = 
              [self.storyboard instantiateViewControllerWithIdentifier:@"Calculate"];
calculationController.translatesAutoresizingMaskIntoConstraints = NO;
[self addChildViewController:calculationController];
[self.calcualtionView addSubview:calculationController.view];
[calculationViewController didMoveToParentViewController:self];

// Add constraints for the view to fill calculationView
UIView *calcView = calculationController.view;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(calcView);
NSArray *hConstraint = 
       [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[calcView]|" 
                                               options:0 metrics:nil
                                                 views:viewsDictionary];
[self.calculationView addConstraints:hConstraint];
NSArray *vConstraint = 
       [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[calcView]|" 
                                               options:0 metrics:nil
                                                 views:viewsDictionary];
[self.calculationView addConstraints:vConstraint];
查看更多
登录 后发表回答