addChildViewController 不适合 UIViewController
addChildViewController don't fit UIViewController
受到 Scott Sherwood tutorial 的启发,我在 UIViewController 中有一个 UIView,它通过自定义 segues 收取不同的 UItableviewController/UIViewController。
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];
}
第一个segue在viewDidLoad方法中调用
controller.m
- (void)viewDidLoad {
[super viewDidLoad];
[self performSegueWithIdentifier:@"dashboardNewPon" sender:[self.buttons.subviews objectAtIndex:0]];
}
然后画面就这样开始了
其他 segues,有此行为
我不明白为什么,但它肯定会自动布局。
谢谢
我认为你应该添加这一行
CGRect dstFrame = CGRectMake (0, 0, controller.container.frame.size.width, controller.container.frame.size.height);
dst.view.frame = dstFrame;
之后
controller.currentViewController = dst;
并在 viewWillAppear 中调用第一个 segue:
我通过阅读这篇文章找到了解决方案 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]];
受到 Scott Sherwood tutorial 的启发,我在 UIViewController 中有一个 UIView,它通过自定义 segues 收取不同的 UItableviewController/UIViewController。
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];
}
第一个segue在viewDidLoad方法中调用
controller.m
- (void)viewDidLoad {
[super viewDidLoad];
[self performSegueWithIdentifier:@"dashboardNewPon" sender:[self.buttons.subviews objectAtIndex:0]];
}
然后画面就这样开始了
其他 segues,有此行为
我不明白为什么,但它肯定会自动布局。
谢谢
我认为你应该添加这一行
CGRect dstFrame = CGRectMake (0, 0, controller.container.frame.size.width, controller.container.frame.size.height);
dst.view.frame = dstFrame;
之后
controller.currentViewController = dst;
并在 viewWillAppear 中调用第一个 segue:
我通过阅读这篇文章找到了解决方案 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]];