content doesn't rotate if I use UINavigationController - ios

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.
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.

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.

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.

Related

How to instantiate and push an (additional) storyboard into my app?

My app is mainly constructed without storyboards.
However, I have created just one for the registration of users.
This storyboard consists of a navigation controller containing 2 views controllers.
The first one is called "FirstVC", and the one "SecondVC" (this one would not be used for my question).
I am trying to push it in the AppDelegate.m file (trying various ways, see below) but my screen is always black.
attempt 1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"FirstRegistration" bundle:nil];
FirstVC *fvc = [sb instantiateViewControllerWithIdentifier:#"FirstVC"];
UINavigationController *navController =(UINavigationController *)self.window.rootViewController;
[navController pushViewController:fvc animated:YES];
return YES;
}
attempt 2
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"FirstRegistration" bundle:nil];
FirstVC *fvc = [sb instantiateViewControllerWithIdentifier:#"FirstVC"];
self.window.rootViewController = fvc;
return YES;
}
Note : I've set the FirstVC scene to FirstVC in the identifier Custom Class section and also set the storyboard ID to FirstVC. However I have not touched to the NavigationController scene, should I just set the custom class to UINavigationController ?
can you try adding this line
[self.window makeKeyAndVisible];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"FirstRegistration" bundle:nil];
FirstVC *fvc = [sb instantiateViewControllerWithIdentifier:#"FirstVC"];
self.window.rootViewController = fvc;
[self.window makeKeyAndVisible]; //**Add this**
return YES;
}

Got SIGABRT when running my first iOS app

Trying to write my first iPhone application using Xcode 5.1. Here is part of my AppDelegate code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AddViewController"];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.viewController];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I have a storyboard called "Main.Storyboard" with Navigation Controller and View Controller.
When I run my app I get SIGABRT error with NSInternalInconsistencyException exception.
Could anyone help me to fix this error?
Thanks.
why you adding subview as a UINavigationController.view and root-view as a view-controller? Your code must be look like this:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AddViewController"];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.navigationController];
[self.window makeKeyAndVisible];
return YES;
}
And verified that identifier of your viewcontroller is setting correct or not that you are given in above.
Try this
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyBord = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:[NSBundle mainBundle]];
self.window.rootViewController = [storyBord instantiateInitialViewController] ;
[self.window makeKeyAndVisible];
return YES;
}
If you use storyboard as a main interface you don't need to setup any code. So you can add UINavigationController to the scene in you storyboard. Then set rootViewController using connection inspector.
Check the video for understanding: link
the code will look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

Trasform ( or Wrap ) UIViewController into UINavigationController

I try to integrate this sdk ( http://whamcitylights.com/sdk/#iOS )
in a my storyboard application,
My problem is in the point 4.
NOTE: That code assumes you are using a UINavigationController. If necessary you may need to add code to create and initialize it.
Here is an example:
I have UIPageViewController with four different UIViewController
and I need to change the last one with a UINavigationController
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = ... // however you create your view controller...
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[self.window makeKeyAndVisible];
return YES;
}
I need to do this in a storyboarded-based application
Thank you all!
Use it:
[self.navigationController pushViewController:nextViewController animated:YES];

how do I set the orientation of a UIWindow with UINavigation

I'm adding the following code to my application in order to navigate back and forth from a quicklook view.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self rotateView];
nav = [[UINavigationController alloc] initWithRootViewController:self];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
[[self navigationController] setNavigationBarHidden:YES];
This code is run in my main ViewController viewDidLoad function. My application runs in landscape but this window and navigation bar loads in portrait mode. My application doesn't have a statusBar so some code I've found doesn't work. I get an error if I try to check and listen for status bar orientation notifications.
I'm looking for a way to allow this navigation view to set it's orientation based on the apps current orientation.
If I understand your question Your above code should be placed in didFinishLaunchingWithOptions not in viewDidLoad in the AppDelegate and the initialization of MainViewController should be there also.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.mainViewController = //init your MainViewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:(UIViewController*)viewController];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}

uinavigationcontroller navigation bar always hidden

I'm developing an iOS 4 application with latest SDK and XCode 4.2.
I'm using an UINavigationController and I don't want to show Navigation Bar. To do that, I use this code on AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
}
else
{
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBar.hidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
But, this line, navController.navigationBar.hidden = YES; doesn't work. I don't see navigation bar on first view controller, but I see it on others views.
Any clue?
I have achieved this doing the following:
Setting navController.NavigationBar.hidden = YES; in AppDelegate, after alloc it.
Setting [navController setNavigationBarHidden:YES animated:YES]; on viewWillAppear: on every viewController that I pust to navController.
try
[navController setNavigationBarHidden:YES animated:NO]
The doc is here.
I think that the navigationBar has to be set hidden on the view, not on the controller.

Resources