onivi
onivi

Reputation: 1348

content doesn't rotate if I use UINavigationController

App content doesn't rotate if I use UINavigationController in my app. Only status bar orientation changes.

This is my didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
                                                 bundle:nil];
    SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:@"specific"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window setRootViewController:navigationController];

    return YES;
}

I dont understand why I am getting this result.

enter image description here

FOUND IT

Finally, I found a solution. But I don't understand why.

this is my didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window.rootViewController =nil;
    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
                                                 bundle:nil];
    SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:@"specific"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:navigationController];

    return YES;
}

And this is viewDidLoad method of SpecificViewController.

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    self.navigationItem.title=@"The Title";

}

And I set "View controller-based status bar appearance" property to NO in plist file. It is working now but I don't understand what do all these mean and why.

Upvotes: 1

Views: 250

Answers (2)

ChrisJP
ChrisJP

Reputation: 975

I don't think the answer you've accepted is the correct one here, I guess if it works for you that's great but it still looks wrong to me.

The reason rotation wasn't working was because you're using Storyboards which automatically create a UIWindow. Then in your application didFinishLaunching method you are alloc'ing a new UIWindow. Only the first UIWindow (created by your storyboard) will receive the rotation notifications, which is why you only noticed the status bar rotating and not your views.

The solution is to remove the following lines from your didFinishLaunchingWithOptions method:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];

Your rootViewController will also be set in the storyboard, there's no need to set it again in code.

Upvotes: 2

amone
amone

Reputation: 3862

This works for me, try:

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

    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
                                                 bundle:nil];
    SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:@"specific"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

add this to SpecificViewController.m

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

set "View controller-based status bar appearance" property to YES and remove "Main" from Main Interface options.You don't need to use setStatusBarHidden method.

Upvotes: 1

Related Questions