ios – 如何在父视图上添加子视图控制器的视图

前端之家收集整理的这篇文章主要介绍了ios – 如何在父视图上添加子视图控制器的视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- (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: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.

解决方法

一些观察:

>你应该关闭translatesAutoresizingMaskIntoConstraints:

childviewcontroller.view.translatesAutoresizingMaskIntoConstraints = NO;

>您还应该定义垂直约束:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[cview]-|" 
                                                                  options:0
                                                                  metrics:nil                                        
                                                                    views:viewsDictionary]];

>与您的问题无关,您不需要为cview创建[[UIView alloc] init].你马上放弃它.
>我不确定你为什么要删除self.view的约束. (我假设你这样做是因为你在测试时撕掉了你的头发.)你不必这样做.但如果你在这里有其他事情让你认为你需要这样做,那么让我们知道那是什么.
>添加子控制器时,调用didMoveToParentViewController,而不是willMoveToParentViewController. addChildViewController为您调用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]];
}

猜你在找的iOS相关文章