为什么 UINavigationController 根控制器的视图小于导航控制器

Why is view of UINavigationController root controller smaller than nav controller

我正在尝试创建一个透明的 UINavigationBar,这是我所做的:

首先,我的代码中有一个 navVC 和一个 general VC 作为 root VC。

SNLoginViewController *loginVC = [[SNLoginViewController alloc] init];
SNLoginNavController *loginNavVC = [[SNLoginNavController alloc] initWithRootViewController:loginVC];
[self presentViewController:loginNavVC animated:YES completion:nil];

我没有覆盖 SNLoginViewControllerinit 方法。

下面是 initWithRootViewController: 的实现:

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
    
        UIImage *bgImage = [UIImage imageNamed:@"bg"];
        UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage];
        bgImageView.frame = [UIScreen mainScreen].bounds;
        [self.view insertSubview:bgImageView atIndex:0];
    
        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"transparent"]//transparent is a transparent png
                             forBarMetrics:UIBarMetricsCompact];
        self.navigationBar.shadowImage = [UIImage imageNamed:@"transparent"];
        self.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    
        [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:18]}];
        self.navigationBar.clipsToBounds = YES;
    }
    return self;
}

使用这些代码 (setBackgroundImage:forBarMetrics:) 我应该能够使导航栏透明,但出了点问题。主要问题是 loginVC 的框架不知何故小于 loginNavVC.


(来源:zybuluo.com

上图中选择的视图是loginVC,左边的是loginNavVcloginVC 小于 loginNavVc

但是在 loginVCviewDidLoad 中我打印了选定的视图,它的框架是 (0 0; 320 568) 但是当我在视图层次结构中打印它的描述时(内存中的相同地址),它的框架是(0 64; 320 504)。为什么会这样?如何全屏(不像游戏那样全屏,状态栏应该还是可见的)?

在展示你的导航控制器之前添加这个。

[self.loginNavVC.navigationBar setBackgroundImage:[UIImage new]
                     forBarMetrics:UIBarMetricsDefault]; 

self.navigationController.navigationBar.shadowImage = [UIImage new];

self. loginNavVC.navigationBar.translucent = YES;