uinavigationcontroller navigation bar always hidden - ios

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.

Related

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

Programmatically creating UINavigationController in iOS

I am new to iOS. And I want to use navigation controller in my application but I have no any idea how to do it. So can any one guide me step by step for creating navigation in my application.
In appDelegate.h
#property (strong, nonatomic) UINavigationController *navController;
and set the delegate UINavigationControllerDelegate and synthesise object in appDelegate.m
now,
appDelegate.m
you can set navigation controller in didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
frstVwCntlr = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
self.window.rootViewController = self.navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In the above code , your firstViewController is set to UINavigationController and UINavigationController added to UIWindow like
self.window.rootViewController = self.navController
Hope this may help you
If you want to create everything programmatically you have to do it in AppDelegate.
But if you don't want to do it programmatically, then just select the ViewController in Storyboard then select menu options:
Editor > Embed In > Navigation Controller
You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.
So for creating a UINavigationController programatically without using storyboards, go to your app delegate and do the following. Create two properties, window and viewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor clearColor];
self.viewController = [[YourFirstViewController alloc] initWithNibName:#"YourFirstViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:#"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];
where ImageViewController2 is a class name
Here is the code that you should write in app delegate.
UIViewController *vc=[[UIViewController alloc]initWithNibName:#"vc1" bundle:nil];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
view.backgroundColor=[UIColor redColor];
[vc setView:view];
self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = self.navme;
For Swift 3.0, using filter:
let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)

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];

iOS unable to load views

I am trying to navigate from one view to another on touch of a button using iOS 5.0.
The code I am using
- (IBAction)Actionlogin:(id)sender {
NSLog(#" login button has been pressed");
NSLog(#"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:#"test_details" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
I have two xib files test_details_iPhone.xib and test_details_iPad.xib
Inside my testdetails.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"it is coming here in second view");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
NSLog(#"it has been returned ");
return self;
}
LOGs
2013-04-25 11:06:17.191 app_gototest[3067:207] login button has been pressed
2013-04-25 11:06:17.194 app_gototest[3067:207] In init
2013-04-25 11:06:17.195 app_gototest[3067:207] it is coming here in second view
2013-04-25 11:06:17.196 app_gototest[3067:207] it has been returned
The view is not getting loaded onto the view. I suppose I am missing something.
application:didFinishLaunchingWithOptions: in appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
** I am trying Jasper Blue's approach**
argc int 1
argv char ** 0xbfffed2c
*argv char * 0xbfffee44
In appDeleagte.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
You code looks OK, so it must be that self.navigationController is nil. . . Have you set up a navigation controller?
For your code to work, your current UIViewController needs to be contained within a UINavigationController. . . you can set up the UINavigationController as the root view controller in your application delegate as follows:
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
NB: You could clean the above code up a little, but you get the picture, which is that you have to set the root view controller to be a navigation controller, like this:
self.window.rootViewController = navigationController;
You could also make a custom root view controller if you like, but the above will let you achieve what you're trying to do - UINavigationController will be used as the navigation system throughout your app.
Your navigation controller is nil. So replace your didFinishLaunchingWithOptions method with
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
UINavigationController *navcontroller = navigationController = [UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcontroller;
[self.window makeKeyAndVisible];
return YES;
}
Now you have a navigation controller so the call to push method on the self.navigationController should push the second controller.
Please try
- (IBAction)Actionlogin:(id)sender {
NSLog(#" login button has been pressed");
NSLog(#"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:#"test_details_iPhone" bunndle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
// test_details_iPhone.xib and test_details_iPad.xib
//Change initWithNibName#"test_details" with test_details_iPhone or test_details_iPad

login view does not load before main ViewController in Xcode 4

Here is the layout of my app.
ApplicationName
LoginViewController.h
LoginViewController.m
LoginView.xib
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController_iPhone.xib
ViewController_iPad.xib
Currently in my AppDelegate.m I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
self.loginViewController = _loginViewController;
[_loginViewController release];
[_window addSubview:[loginViewController view]];
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];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
My LoginView.xib has it's File's Owner defined as LoginViewController.
I was at first getting an error stating: reason: '-[UITableViewController loadView] loaded the "LoginView" nib but didn't get a UITableView.'"
I changed UITableViewController to UIViewController and I was able to run the app without an error. The only problem now is that my LoginViewController does not load. I see the blank grey ViewController_iPad.xib loading.
What am I missing here?
I can post up any other code that would be useful.
Thanks in advance!
You should be setting your window's root view controller to self.loginViewController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
[self.loginViewController release];
self.window.rootViewController = self.loginViewController;
[self.window makeKeyAndVisible];
return YES;
}

Resources