Search code examples
iosipaduiviewcontrollerxcode4

UIViewController being pushed down on screen when displayed using insertSubview


Having a bit of a problem, here's the breakdown:

My AppDelegate uses as it's rootViewController a view controller. This view controller's purpose is to swap a couple of other view controllers in and out, call it login and main screens. For the sake of simplicity I've removed any swapping code from the code that I will post and am having the rootViewController simply show it's first content view controller, say login.

The issue I'm having is that the content view controller seems to be pushed down a little bit inside the rootViewController. I've given the rootViewController a blue background and the content view controller an orange background. Here's my code:

AppDelegate.h

#import <UIKit/UIKit.h>

@class MainViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MainViewController *main;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

@synthesize main;

- (void)dealloc
{
    [_window release];
    [main release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    MainViewController *mainVC = [[MainViewController alloc] init];
    self.main = mainVC;
    self.window.rootViewController = self.main;

    [self.window makeKeyAndVisible];

    return YES;
}

MainViewController.h

#import <UIKit/UIKit.h>

@class ContentViewController;

@interface MainViewController : UIViewController

@property (nonatomic, retain) ContentViewController *content;

@end

MainViewController.m

#import "MainViewController.h"
#import "ContentViewController.h"

@implementation MainViewController

@synthesize content;

- (id)init
{
self = [super init];
    if (self ) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self.view insertSubview:self.content.view atIndex:0];              
}

- (void)dealloc {
    [content release];  
    [super dealloc];
}

@end

ContentViewController.h

#import <UIKit/UIKit.h>

@interface LoginVC : UIViewController

@end

ContentViewController.m

#import "ContentViewController.h"

@implementation ContentViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)dealloc {
    [super dealloc];
}

@end

The above codes gives me a status bar with a blue strip (approximately the height of the status bar) under it (which is the main view controller) followed by the orange content view controller taking up the rest of the screen. For some reason that main view controller seems to be pushing the content view controller down a little.

Strangely, if I create a view controller and use a XIB for drawing the view, it looks just fine and the content view it right up against the top of the main view.

If anybody could shed some light on this issue I'd very much appreciate it.

Cheers


Solution

  • I suppose that you shouldn't be doing

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self.view insertSubview:self.content.view atIndex:0];
    

    but rather:

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self presentViewController:self.content animated:YES completion:nil];