View appears 20 pixels below navigation bar - iphone

When I programmatically create, in the AppDelegate, a UINavigationController, having as root an embedded UITabBarController with for example two simple tabs consisting of standard UIViewControllers, the views of the UIViewControllers appear to be shifted down 20 pixels from the navigation bar, i.e there is a 20 pixels gap between the bottom of the navigation bar and the top of the view.
The code is a dead simple empty application with only the following code in the AppDelegate didFinishLaunchingWithOptions function:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController* vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController* tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]
animated:YES];
UINavigationController* navigationController = [[UINavigationController alloc]
initWithRootViewController:tabBarController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
The problem seems to be linked to the height of the status bar, however I do not understand what I am doing wrong.
The problem does not appear in the simulator though, and only seems to appear when running on the device. Also when you select the second tab, then the views seem to be moved back to it's normal position (without a 20 pixels offset from the navigation bar).
Has anybody experienced a similar problem and/or is there something that I am doing wrong?

As a general recommendation, try (at all cost) to avoid pushing a tabBarController into a NavBarController, it's not recommended by Apple. The "correct" way is to create a NavBarController for every ViewController, and then set the NavController as items for the TabController, for example, based on your code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor blueColor];
UINavigationController *vc1Nav = [[UINavigationController alloc]
initWithRootViewController:vc1];
UIViewController* vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor redColor];
UINavigationController *vc2Nav = [[UINavigationController alloc]
initWithRootViewController:vc2];
UITabBarController* tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:vc1Nav, vc2Nav, nil]
animated:YES];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
Any way, here is the Apple relevant document if you want to check it.

Related

Programmatically creating tab bar for ViewController

I've been looking at programatically adding a tab bar to my view controller because having a scroll view I can't place it on without it being in the middle of my view. I'm abit confused about how to add it. Does it need to be initiated in my ViewDidLoad method or my AppDelegate?
If I have:
UITabBarController *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release];
How can I allocate it to the bottom of my scrollview?
Thanks!
Now in my appDelegate class :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewController* vc = [[ViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
tabBarController.viewControllers = controllers;
[_window addSubview:tabBarController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
It's crashing and not sure if it's a release I'm missing.
Edit:
For anyone else wondering this in Appdelegate.m:
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
Take a look at this Documentation given by Apple: ViewControllers.
Sample Code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UITabBarController *tabBarController = [[UITabBarController alloc] init];
FirstViewController* vc1 = [[FirstViewController alloc] init];
SecondViewController* vc2 = [[SecondViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
I did it this way. I copied this from my App Delegate and it works fine. Basically you add the view controllers to the tab bar then add the tab bar to the window's subview.
Instantiate the instance
iVacationTabBar = [[UITabBarController alloc] init];
However you create the views/view controllers:
UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools];
UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings];
UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about];
UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage];
UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup];
Add the controllers to the array:
NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage, rvc_tools,rvc_settings, rvc_backup, rvc_about, nil];
Set the array of view controllers to your tab bar:
[iVacationTabBar setViewControllers: controllers animated:NO];
Add the tab bar to the window's subview.
[_window addSubview:iVacationTabBar.view];
You can not add a UITabbarController on a UIViewController. Instead, on a ViewController you have to show the TabbarController, create a TabbarController, set ViewControllers for it, and add it to a Window.

navigationcontroller title and button

In my tab based application delegate i added the navigation controller as like the following,
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[view1 alloc] initWithNibName:#"view1" bundle:nil];
UIViewController *viewController4 = [[view2 alloc] initWithNibName:#"view2" bundle:nil];
UIViewController *viewController5 = [[view3 alloc] initWithNibName:#"view3" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4,viewController5];
navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
[self.window addSubview:[navigationController view]];
self.window.rootViewController = self.navigationController;
i would not be able to added the title in each view. While adding it doesn showing the title?Pls help me to resolve this issue
use bellow code...
Here i add UINavigationController to every tab and also assign title for tab and also for UINavigationBar like bellow...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;
UINavigationController *navviewController1 , *navviewController2, *navviewController3, *navviewController4, *navviewController5;
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[[YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navviewController1.title = #"Home";
// navviewController1.navigationBarHidden=YES;
viewController2 = [[[YourViewController2 alloc] initWithNibName:#"YourViewController2" bundle:nil] autorelease];
navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
// navviewController2.navigationBarHidden=YES;
navviewController2.title = #"HowItsWork";
viewController3 = [[[YourViewController3 alloc] initWithNibName:#"YourViewController3" bundle:nil] autorelease];
navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
// navviewController3.navigationBarHidden=YES;
navviewController3.title = #"Join Us";
viewController4 = [[[YourViewController4 alloc] initWithNibName:#"YourViewController4" bundle:nil] autorelease];
navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
// navviewController4.navigationBarHidden=YES;
navviewController4.title = #"Become";
viewController5 = [[[YourViewController5 alloc] initWithNibName:#"YourViewController5" bundle:nil] autorelease];
navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
// navviewController4.navigationBarHidden=YES;
navviewController5.title = #"Contact Us";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
self.window.rootViewController = self.tabBarController;
}
[self.window makeKeyAndVisible];
}
2)OR
Also you can simple assign title in your particular class like bellow...
-(void)viewWillAppear:(BOOL)animated{
self.title = #"yourTitle";
}
i hope this helpful to you...
Go to viewdidload method in every tabbar controller & use line:
navigationController.title=#"hello";
In every viewcontroller viewdidload
self.navigationcontroler.title=#"firstVC";
"title" is the property of UIViewController and UINavigationController is the subclass of UIViewController you can also access it object of UINavigationController, just like navviewController.title = #"myTitle"; and for more detail check Apple Docs. Hope it will help. Cheers
You have added TabBar Controller inside a Navigation Controller so you cannot add title and arises this issue.
Try to add each navigation controller with View controller inside tabbar controller.
I mean add ur ViewController as a root View controller of navigation controller then add to tab Bar controller.
viewController1 = [[[YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
Now then ad this navigation controller inside to tabbar controller. Do this for all other viewcontrollers

Change tab bar item title and color programmaticaly

I have implemented a tab bar controller in my App Delegate, but it's just empty squares in the tab bar. I wish to could change title and images of them and also I want to know how use not only custom image I add, but "default" images implemented in Xcode ("calculator" image, "search" image).
If you have tab bar in a xib, you can see it in tab bar item -> attributes inspector -> Identifier, then there is a list, if you don't want to use custom images. So there is my appDelegate.m code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after app launch
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
UINavigationController *listNavigationController = [[UINavigationController alloc] initWithRootViewController:banksList];
UIViewController *first = [[BIDViewController alloc] initWithNibName:#"BIDViewController" bundle:nil];
UIViewController *second = [[BIDDailyCount alloc] initWithNibName:#"BIDDailyCount" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:first,second,listNavigationController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You have to create your UITabBarItems your self.
In you appdelegate you could do something like:
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
banksList.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
return self;
It might be a good idea to move this to a custom initializer in the controllers for each tab.

Combine UITabBarController with UINavigationController

I tried to use an "tabbed application" with a navigation bar in it. With the default the tab bar works fine, but I just can't gat a navigation bar. I found some stuff about pushing the navigation-bar and stuff like that, but all the stuff I found was some years ago, so don't gonna help me. And the recent stuff is outdated to, since iOS5 and the new version of Xcode..
Could anyone point me in the right direction to combine a to solve this problem?
Keep the following facts in mind please:
I'm developing for iOS5
I'm using Xcode 4.2
Here's how you can achieve it programmatically.
Delete the reference to your main xib in [appName]-Info.plist
In main.m, load your delegate:
int retVal = UIApplicationMain(argc, argv, nil, #"myAppDelegate");
In the app delegate, load the tabBar, the navigation controller and the view in the navigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// View Controllers for tabController (one viewController per tab)
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// first tab has view controller in navigation controller
FirstView *firstView = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView];
[viewControllers addObject:navController];
SecondView *secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[viewControllers addObject:secondView];
// create the tab controller and add the view controllers
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSMutableArray *arrayViewController = [[NSMutableArray alloc] init];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[arrayViewController addObject:navigationController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[arrayViewController addObject:navigationController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = arrayViewController;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In iOS 5 it is no longer tolerated to change the view controller for a tab (no problem before iOS5). The only accepted controller is that defined in IB for that tab. So it is neccessary to install a navigation controller on this tab and give his view the navigation bar. Then you can push or pop your desired views without changing the tab's controller.
The basic theory is that you create a UITabBarController, and then put a UINavigationController inside that, and then put a UIViewController as the root view controller of the navigation controller. bryanmac just answered with a good code sample.

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?
Here the viewcontroller Class is is UITableviewcontroller .
right now if i am adding the tab bar it appears some where in table view. i want to display it on bottom of window and and tableview should scroll separately from Tabbarcontroller
Please help and suggest
Thanks
FirstView *view1 = [[FirstView alloc]init];
SecondView *view2 = [[SecondView alloc] init];
UINavigationController *firstview = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *secondview = [[UINavigationController alloc] initWithRootViewController:view2];
UITabBarController* tabBar = [[UITabBarController alloc] init];
[tabBar setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];
[tabBar setDelegate:self];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBar];
[self.view addSubview:navigationController.view];
Try this. here FirstView, SecondView are the class. if you click the tab, corresponding class (view) will open. And you have declare the tabbar and navigation controller delegate.
first you just select tabbar base application from project template.
now double click on mainwindow.xib its open it in interface builder.
now click on tabbarcontroller and open inspector window.
in above image your are able to see list of viewcontrollers. click on class tab will gives you the option of controller type. see in below image
if your are selecting table view controller then your first controller is table view. and tabbar is not in your view. table is scroll separately.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//************ WRITE THE CODE OVER HERE FOR THE CREATION OF THE TAB BAR BY CODING**************
tabbar_object=[[UITabBarController alloc] init];
self.tabbarViewController=[[[TabBarViewController alloc] initWithNibName:#"TabBarViewController" bundle:nil] autorelease];
self.viewcontroller1=[[[FirstViewControllerForTabBar alloc] initWithNibName:#"FirstViewControllerForTabBar" bundle:nil] autorelease];
self.viewcontroller2=[[[SecondViewControllerForTabBar alloc] initWithNibName:#"SecondViewControllerForTabBar" bundle:nil] autorelease];
UINavigationController *tabbatnavigation=[[UINavigationController alloc] initWithRootViewController:tabbarViewController];
tabbatnavigation.title=#"First Tab";
UINavigationController *navigation1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
navigation1.title=#"Sign Up";
UINavigationController *navigation2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
navigation2.title=#"Sign Out";
tabbar_object.viewControllers=[[NSArray alloc] initWithObjects:navigation1,navigation2,tabbatnavigation, nil];
[tabbar_object setSelectedIndex:0];
//************************************************************************************************
self.window.rootViewController = tabbar_object;
[self.window makeKeyAndVisible];
return YES;
}