0

When I replace my window's rootViewController's view it does not give the correct safeAreaLayoutGuide. I'm primarily trying to figure out how to change the following UIView and then get the correct safeAreaLayoutGuide from it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIWindow* uiwindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController* vc = [[UIViewController alloc] init];

    vc.view = [[UIView alloc] init];
    uiwindow.rootViewController = vc;
    [uiwindow layoutIfNeeded];
    [uiwindow makeKeyAndVisible];

    UILayoutGuide* guide = [vc.view safeAreaLayoutGuide];
    printf("%f %f %f %f\n",[guide layoutFrame].origin.x, [guide layoutFrame].origin.y, [guide layoutFrame].size.width, [guide layoutFrame].size.height);

    [vc.view removeFromSuperview];
    vc.view = [[UIView alloc] init];
    uiwindow.rootViewController = nil;
    uiwindow.rootViewController = vc;
    [uiwindow layoutIfNeeded];

    guide = [vc.view safeAreaLayoutGuide];
    printf("%f %f %f %f\n",[guide layoutFrame].origin.x, [guide layoutFrame].origin.y, [guide layoutFrame].size.width, [guide layoutFrame].size.height);

    return YES;
}

Results:

0.000000 44.000000 375.000000 734.000000
0.000000 0.000000 375.000000 812.000000

I'm using SDL and this is the condensed version of it's iOS initialization code. The first/default view is what gets created with a new SDL window. Then the second view gets created when an OpenGL renderer is created. It replaces the first view but then does not give the correct safeAreaLayoutGuide.

James
  • 359
  • 2
  • 12
  • see this may be helps you https://stackoverflow.com/questions/46441988/autoresizing-for-iphone-x/46442867#46442867 – Anbu.Karthik Sep 30 '17 at 01:29
  • @Anbu.Karthik I'm not sure if that helps. I've noticed that getting the safeAreaLayoutGuide from the UIWindow Does give the correct guide on the iPhone X but it does not give the correct results on an iPhone 8 with a status bar showing. It does not account for the status bar when taken from the UIWindow, but it does from the rootViewController's view. Are you referring to the second part of your solution? I'm not sure if I want to custom set the constraints. Why would the first view's constraints be automatically correct but not the second? – James Sep 30 '17 at 02:09

0 Answers0