2013-07-25 11 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]]; 

} 

Ich möchte die Ansicht von childviewcontroller über die übergeordnete Ansicht hinzufügen. Nach dem Hinzufügen habe ich die Einschränkung gesetzt, aber es funktioniert nicht für mich.So fügen Sie die Ansicht des untergeordneten Ansicht-Controllers über die übergeordnete Ansicht hinzu

Ich bin auch immer Warnungen wie diese

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

Sie müssen nur diese Zeile für das [self.view addSubview: childviewcontroller.view]; –

+0

ich habe es in meiner zweiten Linie, mein Problem ist, dass mein Zwang hier nicht funktioniert. @Puneet –

+0

@DhiyanesKaeYes Wenn Sie diesen verwenden automatisches Layout, werden (a) Sie das @ sind vermisst "V: | - [cview] - |" Satz von Beschränkungen; und (b) Sie würden wahrscheinlich wollen 'cview.translatesAutosizingMaskIntoConstraints = NO'. – Rob

Antwort

13

Einige Beobachtungen:

  1. Sie sollten translatesAutoresizingMaskIntoConstraints deaktivieren:

    childviewcontroller.view.translatesAutoresizingMaskIntoConstraints = NO; 
    
  2. Sie vertikale Beschränkungen definieren sollte auch :

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[cview]-|" 
                        options:0 
                        metrics:nil           
                        views:viewsDictionary]]; 
    
  3. Ohne Bezug zu Ihrem Problem, müssen Sie nicht die [[UIView alloc] init] für erstellen. Sie verwerfen es sofort.

  4. Ich bin mir nicht sicher, warum Sie die Einschränkungen für self.view sind zu entfernen. (Ich bin vorausgesetzt, Sie tat, wie Sie Ihr Haar rissen in Ihre Tests heraus.) Sie das nicht tun müssen. Aber wenn du hier etwas anderes vorhast, das dich denken lässt, dass du das tun musst, lass uns wissen, was das ist.

  5. Beim Hinzufügen eines untergeordneten Controllers rufen Sie didMoveToParentViewController, nicht willMoveToParentViewController. Die addChildViewController ruft willMoveToParentViewController für Sie. Sie benötigen nur die didMove... Wiedergabe.

So:

- (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

danke für deine antwort und jetzt nur ich lerne diese konzepte das ist, warum ich in schwierigkeiten bin. @ Rob –

+0

, die für mich gearbeitet hat, wenn ich wie hier die View-Controller mit NIB-Datei geladen. aber als es vom Storyboard kam. instanziateViewControllerWithIdentifier hat es nicht funktioniert. der untergeordnete Container hat auf die Einschränkungen weder die übergeordnete Ansicht nicht reagiert! – hasan83

+0

@ hasan83 - Wenn Sie 'instantiateViewControllerWithIdentifier' verwenden, ist der Prozess identisch. Ich habe es gerade getestet und es funktioniert gut. Ich wette, dass Sie einige Einschränkungen haben, die nicht richtig definiert sind.Wenn Sie die Quelle Ihres Problems immer noch nicht finden, stellen Sie Ihre eigene Frage mit einem [reproduzierbaren Beispiel] (http://stackoverflow.com/help/mcve) des Problems. – Rob