gpt4 book ai didi

ios - addChildViewController 不适合 UIViewController

转载 作者:行者123 更新时间:2023-11-29 10:27:35 24 4
gpt4 key购买 nike

灵感来自 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];
}

第一个segue是在viewDidLoad方法中调用的

controller.m

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

然后画面就这样开始了

enter image description here

其他segues,有这个行为

enter image description here

我不明白为什么,但它肯定会自动布局。

谢谢

最佳答案

我通过阅读这篇文章找到了解决方案 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]];

关于ios - addChildViewController 不适合 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com