ios - 容器/子 UIViewControllers 和状态栏

标签 ios objective-c uiviewcontroller

我试图让父 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: 中禁用状态栏,我会得到以下信息:

No status bar

如果我启用状态栏,我会得到:

Status bar

有了状态栏,子 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/

相关文章:

objective-c - NSDictionary 和关于 "pointer from integer without a cast"的警告

ios - 我怎么知道 viewcontroller 是因为推送加载还是因为 UINavigationController 弹出后的 View 重建加载?

ios - NSUserDefaults 似乎没有创建全局可访问的变量

ios - UITableViewCell 的 backgroundView 从 UITableView 的左边缘偏移

ios - AES 加密/解密 iOs 和 .Net

ios - 访问相机和照片库

c++ - 对 16 位灰度垫进行着色仅产生一半图像

ios - 是什么导致了 <Error> : CGContextSetBaseCTM: invalid context 0x0 when implementing loadView?

ios - UIGestureRecognizer 知道调用它的对象是什么吗?

iOS:删除核心数据中具有一对多关系的实体