Custom UITabBarController Problems with View Controllers and Views - iphone

I'm writing a custom UITabBarController so I can fully control appearance of the tab bar. I've got it all working so I have an array of view controllers that it handles.
The controller has a main view which fills the screen, and inside it it has a UIView at the bottom for the tab bar. That tab bar view has a button for each view controller. When buttons are pressed I add the view controller's view to the main view, and set it's frame so that it doesn't cover the tab bar view:
controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kTabBarHeight);
This all works fine, and I can flick between the view controllers just fine. However, when I present a modal view controller, and then dismiss it, the current view controller's view becomes full screen and covers up my tab bar! I've tried setting the autoresizing masks to not resize, but is keeps happening.
I have also tried adding the view controllers view's to the bottom (below the tab bar) by using:
[self.view insertSubview:controller.view atIndex:0];
But when I do that, the tab bar is even visible above any modal views! Which is strange. I think there's something I'm not understanding so I would be grateful if someone can explain what I'm missing!
Thanks,
Mike

Try setting
controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kTabBarHeight);
in the controller's viewWillAppear method

Try this out. I think you want dynamic view controllers within tab bar controller.
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
tabBarController.delegate=self;
tabBarController=[[UITabBarController alloc] init];
mainDashBoard=[[DashBoard alloc] initWithNibName:#"DashBoard" bundle:nil];
mainSearchView=[[SearchView alloc] initWithNibName:#"SearchView" bundle:nil];
mainMoreView=[[MoreView alloc] initWithNibName:#"MoreView" bundle:nil];
UINavigationController *nvCtr0=[[[UINavigationController alloc] init] autorelease];
UINavigationController *nvCtr1=[[[UINavigationController alloc] initWithRootViewController:mainDashBoard] autorelease];
UINavigationController *nvCtr2=[[[UINavigationController alloc] initWithRootViewController:mainSearchView] autorelease];
UINavigationController *nvCtr3=[[[UINavigationController alloc] initWithRootViewController:mainMoreView] autorelease];
UINavigationController *nvCtr4=[[[UINavigationController alloc] init] autorelease];//[[[UINavigationController alloc] initWithRootViewController:nil] autorelease];
tabBarController.viewControllers=[NSArray arrayWithObjects:nvCtr0,nvCtr1,nvCtr2,nvCtr3,nvCtr4,nil];
nvCtr0.tabBarItem.enabled=NO;
nvCtr4.tabBarItem.enabled=NO;
[window tabBarController.view];
}

I've managed to find a better way to control the appearance of the tab bar by simply inserting subviews to the top of the tab controllers tab bar. It's worked a treat!

Related

How to keep UINavigationController's UINavigationBar transparent when modally presenting a controller?

I've got a fullscreen view inside of a UINavigationController. When I attempt to present a modal view on top of it, the UINavigationBar changes to opaque, pushing down the content, before the modal view animates. How do I keep this from happening?
ContextMenuViewController *cmvc =
[[ContextMenuViewController alloc] initWithNibName:nil bundle:nil];
[cmvc setDelegate:self];
UINavigationController *navControl =
[[UINavigationController alloc] initWithRootViewController:cmvc];
[cmvc release];
[navControl.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[self.navigationController presentModalViewController:navControl animated:YES];
[navControl release];
[[UIApplication sharedApplication]
setStatusBarStyle:UIStatusBarStyleBlackTranslucent
animated:NO];
The UINavigationController's root view does not have any transparency (status bar nor UINavigationBar), only the pushed controllers have the transparency.
I created a video of the issue: http://www.youtube.com/watch?v=KSFvzTR5Ejk
Example source at: http://cl.ly/7lu2
I tried your code in a very small test project and didn't see the issue you describe. I suggest you do the same thing. Start with the Navigation-based Application template. In the main nib, check the navigation controller's Wants Full Screen and Resize View From Nib, and make its nav bar transparent. In the root view controller's nib, put a button that you can respond to, set up the action, and paste in your code. Create the ContextMenuViewController class; there is no need to give it a nib.
Run the app and press the button. The modal view slides into place, with a transparent nav bar, without affecting the transparency of the nav bar that already exists and without moving the existing content.
So now, once you've proved to yourself that it works in this simple project, it's just a question of locating what you're doing different from that in the real project.
Try setting the bar styles during viewDidLoad for the root View Controller.
HERE YOU GO )
OptionsViewController *detailViewController = [[OptionsViewController
alloc] initWithNibName:#"OptionsViewController" bundle:nil];
UINavigationController *optionsController = [[UINavigationController
alloc] initWithRootViewController:detailViewController];
[detailViewController release];
optionsController.navigationBar.translucent = YES;
optionsController.navigationBar.opaque = YES;
optionsController.navigationBar.tintColor = [UIColor clearColor];
optionsController.navigationBar.backgroundColor = [UIColor
clearColor];
optionsController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:optionsController animated:YES];
[optionsController release];

Maintain Title Bar in ModalViewController

When using the modalViewController, I found that that only way to keep a title bar and buttons on the screen that I jump to was to create a navigation controller and set the modalView screen as the rootView of the navigation controller (as below).
userNameViewController = [[UserNameViewController alloc] init];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:userNameViewController];
[self.navigationController presentModalViewController:cntrol animated:YES];
[userNameViewController release];
[cntrol release];
Why can't I simply this by using the original viewController as the modalView (as below)?
userNameViewController = [[UserNameViewController alloc] init];
[self presentModalViewController:userNameViewController animated:YES];
Because the navigation bar you usually see belongs to the navigation controller, not to its children view controllers.
You could create your own navigation bar and add it to your controller's view. However, it would require some relayout (and that's probably why it's easier to just recreate a navigation controller).

Child view offset by 20 pixels in TabBar application

I have a tab bar application and when I display a modal view controller, the content screen is offset by about 20 pixels to the top and left. It looks like this:
I'm presenting this view from the child view controller (detail view) of navigation controller (main view) of the tabview.
When I show the view, I'm hiding the tab bar and navigation bar but status bar is kept visible. Adjusting the view to be centered (through Interface Builder's Simulated Interface Elements->View Mode : Center) and changing the view's frame after a call to 'viewDidLoad' in the controller doesn't seem to shift it.
- (void)viewDidLoad {
// this still doesn't cause it to shift
self.view.frame = CGRectMake(0, 20, 320, 460);
}
What's the way to adjust this so that the content area is shown correctly?
I launch the child view like this:
[detailController presentModalViewController:tvc animated:NO];
The app's view controller hierarchy:
Tab view with two child navigation controllers are created in the app delegate and the nav controllers added to the TabBar's view controllers:
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:tab1ViewController,
tab2ViewController, nil];
[window addSubview:tabBarController.view];
Each view controllers of the tab is created as a NavigationController with 1 view controller:
// MainViewController inherits from UIViewController
[MainViewController *viewController = [[MainViewController alloc] initWithNib..];
tab1ViewController.viewControllers = [NSArray arrayWithObject:viewController];
A detail view controller is launched with 'pushViewController' as a result of some action on tab1ViewController :
DetailController *detailController = [[DetailController alloc]
initWithNibName:#"DetailView"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:detailController animated:YES];
[detailController release];
It's from the detailController that I'm trying to launch the problem controller.
Some things to check right off: is "viewDidLoad" actually getting called?
If so, what is self.view.frame set to after the assignment?
Put an NSLog at the end that prints out the x, y, width, height, and see what's there.
Also, since the trouble vc is modal, it will occupy the entire screen.
"On iPhone and iPod touch devices, the view of modalViewController is always presented full screen."
HTH,
Mike

UITabBar's navigationbar hides navigationbar when view is presented modally

I have app with UItabBarTemplate with navigation controller.
On selecting tab bar ViewControllerA is shown which on button touch pushes UIPieChartTabController which inherits "UIViewController".
Now I want another tab bar in UIPieChartTabController.
so in viewDidLoad of UIPieChartTabController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
UITabBarController *tabbar= [[UITabBarController alloc] init];
tabbar.view.frame = CGRectMake(0, 0, 320, 460);
piechartViewController *pr=[[piechartViewController alloc]init];
pr.tagInAction=1;
pr.title=#"Type";
pr.tabBarItem.image=[UIImage imageNamed:#"trend.png"];
pr.sDate=sDate;
pr.nDate=nDate;
piechartViewController *pr1=[[piechartViewController alloc]init];
pr1.title=#"category";
pr1.tagInAction=4;
pr1.sDate=sDate;
pr1.nDate=nDate;
piechartViewController *pr2=[[piechartViewController alloc]init];
pr2.title=#"paidWith";
pr2.tagInAction=3;
pr2.sDate=sDate;
pr2.nDate=nDate;
//tabbar.tabBar.delegate=self;
//this gave me error
ExportRep *pr3=[[ExportRep alloc]init];
pr3.tabBarItem.image=[UIImage imageNamed:#"database.png"];
pr3.title=#"Export Expenses";
[tabbar setViewControllers:[NSArray arrayWithObjects:pr,pr1,pr2,pr3,nil]];
[self.view addSubview:tabbar.view];
[pr release];
[pr1 release];
[pr2 release];
}
This piece of code worked but now when I select tab of viewController ExportRep type I tried
[self presentModalViewController:objMFMailComposeViewController animated:YES];
but navigationController of objMFMailComposeViewController hides behind navigationController of view that is presenting objMFMailComposeViewController.
Also viewWillAppear viewDidAppear of all the view controller which are bound to tab bar never gets called.
But none of this problem occurs for tabbar and viewcontroller which gets created by UITabbarTemplate.
Why Is it so? Whats wrong when I create Tab bar?
placing another tabBar within a tabbarVC, is not recommended. Why not use a UIToolBar to do the swapping of Views in your PieChartVC instead?
-apart from that, the reason your code doesn't call viewWillAppear,viewDidAppear is because of this:
[tabbar setViewControllers:[NSArray arrayWithObjects:pr,pr1,pr2,pr3,nil]];
[self.view addSubview:tabbar.view];
here only loadView of those prs will be called.
the viewControllers you assign to the tabBars should instead be wrapped around UINavigationControllers.
So something like this instead would do the trick
UINavigationController *nc1 = [[UINavigationController alloc]initWithRootViewController:pr];
[nc1.view setFrame:CGRectMake:("the frame in which you wnt prs to be displayed")];
[pr.view setFrame:nc1.view.frame];
. // similarly assign NavControllers for all prs
.
.
.
[tabbar setViewControllers:[NSArray arrayWithObjects:nc,nc1,nc2,nc3,nil]];
[self.view addSubview:tabbar.view];
The reason why new view which is being presented modally got its navigation bar hidden lies in [self.view addSubview:tabbar.view];
so it got room to present its view only in parent controllers view thus got cut its navigation bar.
so to hack it I kept tab selected and instead of presenting it in selected view controller,have presented it in main view controller only.

How to hide a tabbar at the app startup?

So, I want my app starts with a UIViewController(without seeing a tabbar), and then enter a UITableView with navigationbar and tabbar. the problem is that the Tabbar is visible at the app starts up, anyone can help on this will be very appreciated...
I think you should either send -presentModalViewController:animated: to your main UIViewController with the tab bar controller as an argument or just do this:
[myWindow addSubview: myTabBarController.view];
Make your app a navigation based application (rather than a tab bar based one) then add a tab bar on the UITableView.
There is help for adding the UITabBar here
I do it like this : in this case drawing a table view and map view (From the Locati application)
tabBarController = [[UITabBarController alloc] init]; // creates your tab bar so you can add everything else to it
searchTableViewController = [[SearchTableViewController alloc] init]; // creates your table view - this should be a UIViewController with a table view in it, or UITableViewController
UINavigationController *searchTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchTableViewController] autorelease];
[searchTableViewController release]; // creates your table view's navigation controller, then adds the view controller you made. Note I then let go of the view controller as the navigation controller now holds onto it
searchMapViewController = [[SearchMapViewController alloc] init];
UINavigationController *mapTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchMapViewController] autorelease];
[searchMapViewController release]; // does exactly the same as the first round, but for your second tab at the bottom of the bar.
tabBarController.viewControllers = [NSArray arrayWithObjects:searchTableNavController, mapTableNavController, nil]; //add both of your navigation controllers to the tab bar. You can put as many controllers on as you like
I found this pattern a long time ago. Sorry that I can't point at the original.
YOu then need to add the tabbarcontoller to the relevant view ([...view addSubView:tabBarController];) possibly setting frame first.