我试图让父 View Controller 显示 subview Controller 的 View 。我在 parent 的 viewDidLoad
中有以下内容:
-(void)viewDidLoad
{
[super viewDidLoad];
// create child view controller
[self setMenuController:[[MenuViewController alloc] initWithNibName:@"MenuView" bundle:nil]];
[self addChildViewController:[self menuController]];
// move the child view down 30 pt
CGRect frame = [[[self menuController] view] frame];
frame.origin.y = 30;
[[[self menuController] view] setFrame:frame];
[[self menuController] setWantsFullScreenLayout:YES];
[[self view] addSubview:[[self menuController] view]];
[[[self menuController] view] setNeedsLayout];
}
subview Controller 的 xib 只是一个红色 View ,带有一个居中的、部分透明的 UIImageView
(以查看重叠部分)。
如果我在 application:didFinishLaunchingWithOptions:
中禁用状态栏,我会得到以下信息:
如果我启用状态栏,我会得到:
有了状态栏,子 Controller 的 UIView
会自动调整,以便为状态栏所在的位置腾出空间(因为 ImageView 居中,顶部和底部各有 10 pts)。因为这是一个 subview ,所以无论父 View 是否有状态栏,它都应该始终使用 View Controller View 的完整尺寸。除了将任何 subview 的大小调整为大 20 像素,或者在 subview Controller 中使用包装器 View 之外,还有什么方法可以实现这一目标吗?
最佳答案
尝试将 frame 设置为 menuController View 的边界而不是它的 frame。另外,我不确定您是否需要 setWantsFullScreen ,这会导致 View 重叠在状态栏上。
编辑后:
像这样尝试一下,看看是否可行。
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self setMenuController:[[MenuViewController alloc] initWithNibName:@"MenuView" bundle:nil]];
[self addChildViewController:menuController];
[menuController didMoveToParentViewController:self];
menuController.view.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y +30, menuController.view.frame.size.width, menuController.view.frame.size.height);
[self.view addSubview:menuController.view];
}
关于ios - 容器/子 UIViewControllers 和状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16172694/