UIToolbar in RootViewController

房东的猫 提交于 2019-12-12 03:15:46

问题


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.


回答1:


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.




回答2:


I do not believe you say

toolbar = [UIToolbar new];

Also, have you googled how to make a UIToolbar programmatically?




回答3:


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.

I can think of two ways to do this:

  1. Use the toolbar provided by the navigation controller. You shouldn't try to access that toolbar directly -- use UIViewController's -setToolbarItems:animated: method to set up the items you want for each view controller. If you give each view controller the same set of toolbar items, you'll get the appearance that you're after. I did notice a slight flicker in my quick experiment, but this is a very quick and easy way to get a standard toolbar for all your views.

  2. Create your own container controller. With the introduction of iOS 5 we got the ability to write our own container controllers, i.e. view controllers that can host other view controllers inside themselves, just like UINavigationController and UITabBarController do. So, you could easily write your own container and have it host your toolbar. It sounds like this is what you're already attempting to do, but you have to realize that you don't get that functionality automatically.



来源:https://stackoverflow.com/questions/10668636/uitoolbar-in-rootviewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!