Note that there are some explanatory texts on larger screens.

plurals
  1. POUIWindow subview disappears after when application becomes active
    primarykey
    data
    text
    <p>So, I am trying to achieve having a fixed top banner on Application UIWindow and a working navigation of multiple view controllers. I know there are other ways of performing this but for my project requirements I need to use xibs.</p> <p>The code below allows to achieve this:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.headerView = [[UIImageView alloc] init]; self.headerView.frame = CGRectMake(0.0f, 20.0f, 320.0f, 44.0f); self.headerView.image = [UIImage imageNamed: @"header-banner-background.png"]; [self.window addSubview:self.headerView]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; return YES; } </code></pre> <p>Then in each ViewController viewWillAppear method I have resized both self.view and self.navigationController.navigationBar.frame in order to show themselves below the banner:</p> <pre><code>self.navigationController.navigationBar.frame = CGRectMake(0, 64, 320, 44); self.view.frame = CGRectMake(0, 64, 320, 396); </code></pre> <p>This is fine, works exactly the way I wanted. </p> <p><strong>But if the user presses the home button and the application resigns active, when it becomes active again the last viewController seen get's displayed but without the UIWindow banner, and the self.view and navigationBar are displayed on top.</strong> </p> <p>Of course the current UIViewController viewWillAppear method is not called when the application returns active, but even if I put the resize code in another function and call it through a notification it does not work. But when the user presses a bar item and moves in the navigation stack, then everything works again and UIWindow displays the headerView. </p> <p><strong>Anyone understands why the UIWindow headerView disappears when application returns active?</strong></p> <p><strong>EDIT 1: Possible solution?</strong></p> <pre><code>@interface RootViewController () { ViewController *viewController; UINavigationController *navigationController; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; } return self; } - (void)loadView { // Implement loadView to create a view hierarchy programmatically, without using a nib. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [imageView setImage:[UIImage imageNamed:@"header"]]; [view addSubview:imageView]; navigationController.navigationBar.frame = CGRectMake(0, 44, 320, 44); [view addSubview:navigationController.navigationBar]; self.view = view; } </code></pre> <p><strong>EDIT 2: Second possible solution, still not working</strong></p> <pre><code>- (void)loadView { NSLog(@"loadView root"); // Implement loadView to create a view hierarchy programmatically, without using a nib. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [imageView setImage:[UIImage imageNamed:@"banner"]]; [view addSubview:imageView]; navigationController = [[UINavigationController alloc] init]; navigationController.navigationBar.frame = CGRectMake(0, 43, 320, 44); [view addSubview:navigationController.navigationBar]; [self addChildViewController:navigationController]; self.view = view; [self.view setBackgroundColor:[UIColor yellowColor]]; } </code></pre> <p>This is the result, which seems fine, without the push of the viewController1 (note the yellow background is from the rootViewController):</p> <p><img src="https://i.stack.imgur.com/vM50G.png" alt="Without push"></p> <p>This instead is the result of adding:</p> <pre><code>- (void)viewDidLoad { viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil]; [navigationController pushViewController:viewController1 animated:YES]; } </code></pre> <p><img src="https://i.stack.imgur.com/NeesD.png" alt="With PUSH"></p> <p>The new view controller is not pushed (the background should be black) and the navigation bar has disappeared.</p> <p><strong>EDIT 3: Olloqui solution, still not working</strong></p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window setBackgroundColor:[UIColor redColor]]; UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [self.window addSubview:bannerView]; UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)]; ViewController1 *viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1]; self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44); [navigationView addSubview:self.navigationController.navigationBar]; [self.window addSubview:navigationView]; self.window.rootViewController = self.navigationController; self.window.autoresizesSubviews = YES; [self.window makeKeyAndVisible]; return YES; } </code></pre> <p>Result is I have only the banner, viewController1 is pushed but no navigation bar is displayed. I really need to figure this out, anyone has a different approach?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload