Search code examples
iphoneuitoolbar

UIToolbar in RootViewController


I need UIToolbar when my application launches. It will be on every screen through out the application. So for that i have add it in RootViewController.

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize mainViewController = _mainViewController;

- (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 = [[[MainViewController alloc]init]autorelease];
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}

Currently i have UIToolbar in MainViewController

 // create the UIToolbar at the bottom of the MainViewController
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackOpaque;
// size up the toolbar and set its frame
toolbar.frame = CGRectMake(0, 425, 320, 40);
[self.view addSubview:toolbar];

In all my other UIViewControllers i m adding it like this

[self.view addSubview:toolbar]; 

which i don't want. I want UIToolbar to show automatically in all my other UIViewControllers after attaching it to rootviewcontroller. so that when i flip UIViewController to change to another UIViewController. Only UIViewController should flip not UIToolbar.Right now UIToolbar is also flipping with UIViewController. So that is the reason i want to attach it to the rootviewcontroller.

So how can i add UIToolbar in APPDelegate file and attach it to rootviewcontroller.

Thanks for help.


Solution

  • Everything looks okay in your code except when you create the UIToolbar, for some reason at that point you make your own syntax. This is how you create a UIToolBar

    Example:

    UIToolbar *toolBar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44);
    

    Seriously, your code would not compile, and you would know at exactly what line. Connect the dots and Google how to create a UIToolbar.

    Edit:

    For the Toolbar being display in every view you have a few options:

    (1) Are you sure you don't want a UINavigationController & UINavigationBar?

    (2) Attach it at the UIWindow (i.e. add two subviews to the window). This has to be created with code, I do not believe interface builder is going to work.

    (3) Just create it when each view loads in either (init or viewDidLoad methods)

    Okay, so you would have to add the Toolbar to each view controller (i.e. addSubView). The only other way is to add two subViews to the AppDelegate.