iOS - 在花药UIViewController中呈现UIViewController [英] iOS - Presenting a UIViewController inside anther UIViewController

查看:107
本文介绍了iOS - 在花药UIViewController中呈现UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个话题没有很好的理解,所以请耐心等待我

我试图在另一个内部显示一个UIViewController UIViewController中。我觉得那里没有足够的例子。我已经阅读了文档,但对于iOS来说还是比较新的我有点迷失。

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.

这是我的问题:我可以让孩子UIViewController出现在父UIViewController中但是子视图显示不正确。

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

抱歉,我无法发布图片。

这是我的孩子 - UIViewController

这是我的父母 - UIViewController 。 (蓝色区域是添加子视图的位置)

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)

以下是结果:

Blue UIView到位 - http://i.imgur.com/4vny8EZ.png

如果我将蓝色UIView移到右边这里会发生什么

蓝色UiView移到右边 - 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.

这是我加载的方式我父UIViewControllers类中的子UiViewController:

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!

另外,抱歉我只能发布两个链接。

推荐答案

快速回答:在 viewWillAppear 之前,不会确定视图大小。在那里设置框架它会起作用。

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

更长的答案:

1)如果您使用的是iOS6,我建议您使用 NSLayoutConstraints 来处理视图大小和定位,而不是手动设置视图帧。实际上需要花一点时间才能真正地围绕着它们,但是一旦你做到了,它们比设置帧更加强大和灵活,你不必担心你的 IBOutlets 已创建

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)您没有正确添加子视图控制器。添加视图后,必须调用 didMoveToParentViewController:。使用缺少的代码行以及使用约束而不是框架,您的代码如下所示:

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];

这篇关于iOS - 在花药UIViewController中呈现UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆