gpt4 book ai didi

iOS: subview Controller 和自动布局

转载 作者:可可西里 更新时间:2023-11-01 05:22:01 24 4
gpt4 key购买 nike

我有一个非常简单的场景,但不适合我。我有一个带有两个 View Controller 的 Storyboard:

ViewController1
- ViewA
- ViewB

ViewController2
- ViewC

我的 ViewController1 正在按预期工作,当我旋转设备时, View 会根据我定义的自动布局正确调整。

我的 ViewController2 有一个自由尺寸 (768x722),我取消选中“Resize View From Nib”选项。

这是我的ViewController1 didLoad

- (void)viewDidLoad
{
[super viewDidLoad];

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *vc2 = [story instantiateViewControllerWithIdentifier:@"vc2"];

[self addChildViewController:vc2];
[vc2 didMoveToParentViewController:self];
[self.viewA addSubview:vc2.view];
}

当我旋转设备时,ViewControlled2 中的方法 - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 被调用,它显示了正确的方向。但是,ViewC 不会自行调整大小,也不遵循我的自动布局设置。

有人知道我该如何解决吗?我只想在 ViewA 中添加我的 ViewController2 并让自动布局为 ViewController2 的 View 工作。我只需要一个适用于 iOS 7 的解决方案,因此不需要与早期版本兼容。

谁能帮帮我?

最佳答案

我找到了解决方案,我会在这里注册,以备不时之需。

我创建了一个 UIViewController 属性来保存我的 ViewController2。之后,我添加了以下方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.vc2.view.frame = self.viewA.bounds;
}

关于iOS: subview Controller 和自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19571576/

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