addChildViewController不适合UIViewController

时间:2015-07-11 20:16:39

标签: ios objective-c uistoryboard

Scott Sherwood tutorial启发我在UIViewController中有一个UIView,它通过自定义segues为不同的UItableviewController / UIViewController充电。

enter image description here

segue.m

- (void) perform {

DashboardViewController *controller = (DashboardViewController *)self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;

for(UIView *view in controller.container.subviews){
    [view removeFromSuperview];
}


controller.currentViewController = dst;
[controller addChildViewController:dst];
[controller.container addSubview:dst.view];
}

在viewDidLoad方法中调用第一个segue

Controller.m或者

- (void)viewDidLoad {
[super viewDidLoad];
[self performSegueWithIdentifier:@"dashboardNewPon" sender:[self.buttons.subviews objectAtIndex:0]];
}

并以这种方式启动屏幕

enter image description here

其他segues,有这种行为

enter image description here

我无法理解为什么,但它肯定会自动布局。

由于

2 个答案:

答案 0 :(得分:1)

我认为你应该添加这一行

CGRect dstFrame = CGRectMake (0, 0, controller.container.frame.size.width, controller.container.frame.size.height);    
dst.view.frame = dstFrame;

controller.currentViewController = dst;

并在viewWillAppear中调用第一个segue:

答案 1 :(得分:1)

我通过阅读article找到了解决方案。

此行之后

[controller.container addSubview:dst.view];

我添加了这个

dst.view.translatesAutoresizingMaskIntoConstraints = NO;

[controller.container addConstraint:[NSLayoutConstraint constraintWithItem:dst.view
                                                           attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:controller.container
                                                           attribute:NSLayoutAttributeTop
                                                          multiplier:1.0
                                                            constant:0.0]];

[controller.container addConstraint:[NSLayoutConstraint constraintWithItem:dst.view
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:controller.container
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0
                                                       constant:0.0]];

[controller.container addConstraint:[NSLayoutConstraint constraintWithItem:dst.view
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:controller.container
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0.0]];

[controller.container addConstraint:[NSLayoutConstraint constraintWithItem:dst.view
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:controller.container
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1.0
                                                       constant:0.0]];