2013-07-25 121 views
7
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    ChildViewController *childviewcontroller = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil]; 


    [self addChildViewController:childviewcontroller]; 
    [self.view addSubview:childviewcontroller.view]; 
    [childviewcontroller willMoveToParentViewController:self]; 
    UIView *cview = [[UIView alloc] init]; 
    cview = childviewcontroller.view; 
    [self.view removeConstraints:self.view.constraints]; 

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(cview); 
    [self.view addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"H:|-[cview]-|" 
                options:0 metrics:nil           
                views:viewsDictionary]]; 

} 

我想在父视图上添加childviewcontroller视图。添加后,我设置了约束,但它不适合我。如何在父视图上添加子视图控制器的视图

我也越来越警告这样

2013-07-25 10:47:30.564 neenah[1105:c07] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't 
    understand, refer to the documentation for the UIView 
    property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9256c90 H:|-(NSSpace(20))-[UIView:0x9256a00] (Names: '|':UIView:0x92557a0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x755d690 h=--& v=--& H:[UIView:0x9256a00(320)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9256c90 H:|-(NSSpace(20))-[UIView:0x9256a00] (Names: '|':UIView:0x92557a0)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-07-25 10:47:30.567 neenah[1105:c07] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9256bb0 H:[UIView:0x9256a00]-(NSSpace(20))-| (Names: '|':UIView:0x92557a0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7561690 h=--- v=--- H:[UIWindow:0x92527c0(320)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x755fe50 h=-&- v=-&- UIView:0x92557a0.width == UIWindow:0x92527c0.width>", 
    "<NSAutoresizingMaskLayoutConstraint:0x755d690 h=--& v=--& H:[UIView:0x9256a00(320)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9256bb0 H:[UIView:0x9256a00]-(NSSpace(20))-| (Names: '|':UIView:0x92557a0)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
+0

你只需要这一行是[self.view addSubview:childviewcontroller.view]。 –

+0

我在第二行中做过,我的问题是我的约束在这里不起作用。 @Puneet –

+0

@DhiyanesKaeYes如果你打算使用这个自动布局,(a)你错过@“V:| - [cview] - |”一组约束;和(b)你可能会想'cview.translatesAutosizingMaskIntoConstraints = NO'。 – Rob

回答

13

几个意见:

  1. ,您应该关闭translatesAutoresizingMaskIntoConstraints

    childviewcontroller.view.translatesAutoresizingMaskIntoConstraints = NO; 
    
  2. 你应该定义垂直约束,太:

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[cview]-|" 
                        options:0 
                        metrics:nil           
                        views:viewsDictionary]]; 
    
  3. 与您的问题无关,您无需为cview创建[[UIView alloc] init]。你立即放弃它。

  4. 我不确定为什么要删除self.view的限制。 (我假设你是这样做的,因为你在测试中撕裂你的头发。)你不必这样做。但是如果你在这里有其他事情让你觉得你需要这样做,让我们知道那是什么。

  5. 添加子控制器时,请致电didMoveToParentViewController而不是willMoveToParentViewControlleraddChildViewController为您拨打willMoveToParentViewController。你只需要didMove...呈现。

这样:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // instantiate the view controller 

    ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil]; 

    // or you can instantiate using storyboard 
    // 
    // ChildViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildIdentifier"]; 

    // now do the view controller containment calls to update the view controller hierarchy and add the view as appropriate 

    [self addChildViewController:childViewController]; 
    childViewController.view.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:childViewController.view]; 
    [childViewController didMoveToParentViewController:self]; 
    UIView *childView = childViewController.view; 

    NSDictionary *views = NSDictionaryOfVariableBindings(childView); 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[childView]-|" options:0 metrics:nil views:views]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[childView]-|" options:0 metrics:nil views:views]]; 
} 
+1

谢谢你的回应,现在只有我正在学习这个概念,这就是为什么我有麻烦。 @Rob –

+0

对我来说,当我在这里加载视图控制器与nib文件时工作。但是当它来自故事板时。 instantiateViewControllerWithIdentifier它没有工作。子容器没有响应父约束的约束! – hasan83

+0

@ hasan83 - 当你使用'instantiateViewControllerWithIdentifier'时,这个过程是相同的。我只是测试它,它工作正常。我敢打赌,你有一些没有正确定义的限制。如果您仍未找到问题的根源,请用问题的[可重现示例](http://stackoverflow.com/help/mcve)发布您自己的问题。 – Rob

相关问题