Tab Bar in Iphone Programming? - iphone

I make an application using Tab Bar.
I have 2 views controller: View1 and View2 and a RootViewController.
In RootViewController.m:
- (void)viewDidLoad
{
[self.navigationItem setTitle:#"Tab Bar"];
View1 *vc1 = [[View1 alloc]init];
View2 *vc2 = [[View2 alloc]init];
[vc1.tabBarItem setTitle:#"View1"];
[vc2.tabBarItem setTitle:#"View2"];
[self setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
When I build application, I have an error: Have a white place. When I go to View 2 and back to View 1 it is disappeared. I want to delete white place. Please see image in here: http://photo.ssc.vn/images/108Screen_Shot_2013_08_18.jpg
Thanks for your helping!

This is a common issue when adding view controllers to a tab bar.
The solution is to set wantsFullScreenLayout on the UINavigationController to NO, so it won't even attempt to leave that gap for the status bar.
Apple says :
Tab bar controllers support full-screen layout differently from the
way most other controllers support it. You can still set the
wantsFullScreenLayout property of your custom view controller to YES
if you want its view to underlap the status bar or a navigation bar
(if present). However, setting this property to YES does not cause the
view to underlap the tab bar view. The tab bar controller always
resizes your view to prevent it from underlapping the tab bar.
For more detailed explanation you can see this Question.

Related

Properly manage nav bar and tab bar

The figure shows my storyboard, with segues as directed.
In the tabBar's landing viewcontroller (i.e. I), both tabBar and navBar are visible which is desired. However, if a segue is performed (from I) to go to another viewcontroller (here, II), I want only the navBar. I am able to hide the tabBar by using
self.tabBarController?.tabBar.isHidden = true
Next, I am able to achieve the desired result by adding navigation controller at the beginning as shown below.
This configuration will add a navbar to the viewcontrollers ahead (like splash screen) so I will have to hide the navbar in those viewcontrollers.
Is there other method that does not require to hide the bar(s) and achieve the desired effect?
You should go with your first approach. Tab bar controller have default property to hide bottom bar. See below sample code.
ViewController *viewController = [[ViewController alloc] init];
viewController.hidesBottomBarWhenPushed = YES; // This property needs to be set before pushing viewController to the navigationController's stack.
[self.navigationController pushViewController:viewController animated:YES];
This will hide your tab bar while pushing to any child controllers.
EDIT
You can also set hidesBottomBarwhenpushed from storyboard for your tabbar controller so you don’t have to write down any code.

BarButtonItem not displayed when views of uisplitviewcontroller changed

My app works with tabbarcontroller as root view of the window, where on clicking each tab item loads splitviewcontroller with required views for it. The left and right panes of split views are navigation controllers. Now on any button action or didselectrow in tableview corresponding views are to be loaded in right pane. I succeeded loading views in right pane , but not able to display barbuttonitem when new view controller loaded in right pane of split view.
tabbarcontroller
-->splitviewcontroller
----->Leftpane:navigation controller
--------------->view controllers
----->Rightpane:navigation controller
--------------->view controller
Each Splitview of tab bar wil act like 'iPad Mail app' .
To make the app gernalised, I taken class RootiPadViewController which has the delegate of uisplitviewcontroller and uipopovercontroller which loads alls views in slpitview.
Loaded viewcontroller in right pane of split view as below.
UISplitViewController *splitViewController = (UISplitViewController*)[appDelegate.tabBarController.viewControllers objectAtIndex:tabIndex];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
//[navController pushViewController:viewController animated:YES];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[splitViewController.viewControllers objectAtIndex:0]];
[array addObject:navController];
splitViewController.viewControllers = array;
[array release];
Please suggest me why bar button item not displayed when views changed in splitviewcontroller.
App looks as below
I may be wrong but from my knowledge, the bar button item should appear only when you are in portrait mode, because:
a split view controller has two controllers (master and detail view controller)
both view controllers are shown on the screen when you are in landscape mode
when you are in portrait mode, only the detail view controller is shown, thus the bar button item appears
the bar button item goal is to let you open the master view controller in portrait mode
Please let me know if that helps you.
For Navigation Controller each view should define their left and right bar buttom items, if nothing defined then the tabbar will be empty . The only barbutton Item you will get free is the back barbutton item which appeared when you push a new View Controller above the rootViewController of the navigationController
You have to to allocate them in your ViewDidLoad method of each viewController in the NavigationControoler and set them as right and left barbutton item of your parentViewController(ie navigationController)
Please check this sample project https://github.com/alexth/TBSV
It's about how to use UISplitViewController inside UITabBar.
All logic is in Appdelegate's -loadSplitToTab Just total inheriting of all controllers, in every other cases UISplitViewController needs to be a root (as described in Apple documents) and you will not be able to use UISplitViewController inside UITabBar.

Tab bar seems to be locked (default settings)

I have created a tabbarcontroller (with its default two view controllers) using interface builder in XCode 4.2.
But, when I run the application, the tab bar seems to be locked and I can't choose the other tab. Why is that?
PS: I haven't changed any property of the tab bar or tabbarcontroller in XCode.
How did you go about creating the tab bar? Did you have an initial view and then go to the Edit menu -> Embed In -> Tabbar Controller or did you start with nothing and drag in tab bar controller?
Either way, I just created a project with a single view and tried both ways - but the tab still worked. (if you do it by dragging the tab bar controller from the utility pane, you have to also select 'is initial view controller' if you replace the original view created with the project.
EDIT after your comments:
You don't really need to synthesize the tab bar controller in your AppDelegate - storyboarding will take care of this and you can reference it from code without needing generated synthesizers. Just design the layout in storyboard first by dragging in a tabbar controller (this will automatically create the two view controllers by default). Then select the tabbar controller and under the utilities panel, you'll see the 'is initial view controller' checkbox. Make sure it's checked. Then run your project.
Apologies - assumed because you were using XCode 4.2 that you were using storyboards. Perhaps you could try instantiating a tabbar controller fully in code instead of using IB at all. This is how I usually do it....
// Create the main tabbar controller
tabbarController = [[UITabBarController alloc] init];
// Create the first view controller
MyFirstViewController *v1 = [[MyFirstViewController alloc] initWithNibName:#"MyfirstViewController" bundle:nil];
[v1 setTitle:#"Tab1"];
// Create second view controller
MySecondViewController *v2 = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
[v2 setTitle:#"Tab2"];
// Make an array of controllers for the tabbar
NSArray *tabbarControllerArray = [NSArray arrayWithObjects:v1, v2, nil];
// Set the view controllers used by the tabbar controller
[tabbarController setViewControllers:tabbarControllerArray];
// Release views (retained elsewhere)
[v1 release];
[v2 release];
// Add the controller to the subview
[window addSubview:[tabbarController view]];
// Make key and visible
[self.window makeKeyAndVisible];
Give this way a shot and see how you get on.

Tab Bar in View Based Application - XCode

How can I use Tab Bar in my View Based application in second view ?
EDIT :
(Suppose)My application contains four views. The navigation from firstView to secondView is simple , I want the Tab Bar on the secondView and connect rest of the two view with the Tab Bar.
On the other hand I think this has been discussed here: uitabbarcontroller / uitabbar in navigation based project
Just take a look.
EDIT: If it is a navigation based app and you want your tabbar on the second view, just initialize the navigation controller and use pushViewController message to it and push the tabBarController onto the navigation stack.
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1,viewController2,nil];
[self.navigationController pushViewController:tabBarController animated:YES];
Place this wherever you wish to push this tabBarController from the first view, where the first view is a navigation controller based entity and viewController1,viewController2 etc are the view controllers from the tab bar items.

how to create tabbar programmatically

HI all,
i am having navigation based application, in which i need too implement tab bars ,in one of view.
in one view i need 5 tabs, can any one please suggest me to create tab bars programmatically,, ? each tab should navigate to another xib.
Suggestions are always appreciated.
regards
Here is a sample code from Apple to create tab bar programmatically:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController 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];
}
More here
It's worth noting that a tab bar controller implemented inside a navigation controller is unsupported by Apple. From the same doc linked to by vodhkang above:
Note: Although a navigation controller can be embedded inside a tab, the reverse is not true. Presenting a tab bar interface from within a navigation interface is potentially confusing for users. A navigation interface uses one or more custom view controllers to present an interface focused on one goal, which is usually the management of a specific type of data. By contrast, the tabs of a tab bar interface can reflect completely different purposes in an application and need not be related in any way. In addition, pushing a tab bar controller on a navigation stack would cause the tabs to be displayed for that screen only and not for any others.
So instead of using UITabBarController consider implementing a tab bar with a UIViewController as the tab bar's delegate.