2015-05-19 53 views
7

我通过遏制制作UINavigationController另一个视图控制器的子项。除了在通话状态栏打开后启动应用程序时发生的一个奇怪问题,以及在应用程序用户界面位于屏幕上后将其切换回关闭状态以外,一切正常。在状态栏的位置出现了一个奇怪的黑洞。状态栏大小不正确,包含UINavigationController

考虑下面的自包含的示例应用程序:

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
@property (strong, nonatomic) UIWindow *window; 
@end 

@implementation AppDelegate 
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)opt 
{ 
    // Content 
    UILabel * aLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 200, 40)]; 
    aLbl.text = @"In-call status bar issue"; 

    UIViewController * aContent = [[UIViewController alloc] init]; 
    aContent.title = @"Title"; 
    aContent.view.backgroundColor = UIColor.whiteColor; 
    [aContent.view addSubview:aLbl]; 

    UINavigationController * aNav = [[UINavigationController alloc] 
    initWithRootViewController:aContent]; 

    // Parentmost view controller containing the navigation view controller 
    UIViewController * aParent = [[UIViewController alloc] init]; 
    [aParent.view addSubview:aNav.view]; 
    [aParent addChildViewController:aNav]; 
    [aNav didMoveToParentViewController:aParent]; 

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; 
    self.window.rootViewController = aParent; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 
@end 

int main(int argc, char ** argv) 
    { @autoreleasepool { return UIApplicationMain(argc, argv, nil, @"AppDelegate"); } } 

重现该问题最简单的方法是:

  1. 启动iPhone模拟器。
  2. 按⌘+ Y打开通话状态栏。状态栏将变得宽泛且绿色。
  3. 手动启动应用程序并等待导航控制器显示。
  4. 再次按⌘+ Y可关闭通话状态栏。

的UI现在应该如下所示: Black gap instead of the status bar when switching the in-call status bar back off

任何人都知道如何解决这个问题呢?

+0

您是否尝试将尺寸类别或自动调整大小遮罩设置为“UINavigationContorller”视图?因为应用程序的UIWindow的大小应该超出框框。所以我怀疑问题是导航控制器的'UIView'。 –

+0

我能够按照OP所述重现问题。在应用程序启动后切换通话状态栏会导致预期的行为,但如果在通话状态栏打开后启动应用程序,则切换通话状态栏将导致黑条。 – akivajgordon

+0

我也只是在iOS设备上尝试过,并且问题再次出现,所以它似乎并不特定于模拟器。 – akivajgordon

回答

6

您正在添加一个视图(导航控制器的视图)作为子视图到另一个视图(父视图控制器的视图),而不给它一个框架!这是你应该从来没有做。

这些线路只需添加到您的代码:

aNav.view.frame = aParent.view.bounds; 
aNav.view.autoresizingMask = 
    UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

现在导航控制器的看法有一个框架,并保持它相对于它的父。

+0

第二行不完全是必要的,只是为了解决你提出的问题,但显然你不仅需要一个大小,而且还需要一个规则,如果超级视图是调整大小。在现实生活中,我可能会使用约束而不是'autoresizingMask',但我想保持简短。 – matt

+0

请参阅我的书,了解舞蹈的详细信息,当您执行自定义父视图控制器并添加子视图和视图时,您必须执行此操作:http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers您正在做错误顺序的步骤(尽管我怀疑这是否会影响这个特定的问题)。 – matt

+0

感谢哥们!你让我今天一整天都感觉很好!尽管很简单! – Kerido