编程完成时,ToolBar不显示

我从IB中删除了ToolBar控件,但是试图创build代码。 我尝试了我在网上find的以下代码。 而不是写在“viewWillAppear”这个代码,我有我的代码在同一个UIViewController导航栏中的“栏button项目”。

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //Initialize the toolbar toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault; //Set the toolbar to fit the width of the app. [toolbar sizeToFit]; //Caclulate the height of the toolbar CGFloat toolbarHeight = [toolbar frame].size.height; //Get the bounds of the parent view CGRect rootViewBounds = self.parentViewController.view.bounds; //Get the height of the parent view. CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds); //Get the width of the parent view, CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds); //Create a rectangle for the toolbar CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight); //Reposition and resize the receiver [toolbar setFrame:rectArea]; //Create a button UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]; [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; //Add the toolbar as a subview to the navigation controller. //[self.navigationController.view addSubview:toolbar]; // Instead of adding to a navigation controller (which I don't have), I'm adding directly to the view and is not shown at all. // Hiding the tabBar before I show the toolbar [self.tabBarController.tabBar setHidden:YES]; [self.view addSubview: self.toolbar]; 

我在这里做错了什么? 我必须有info_clicked方法可用(酒吧button项目点击)?

请指教。

这对我有用….

  UIToolbar *toolbar = [[UIToolbar alloc]init]; toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width,44); UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]; [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; [self.view addSubview:toolbar]; [toolBar release]; 

您还需要info_clicked方法:接收button操作。

 - (IBAction)info_clicked:(id)sender{ NSLog("clicked info_button"); } 
 - (void)viewDidLoad { UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithTitle:@"SHOW BAR" style:UIBarButtonItemStyleBordered target:self action:@selector(showToolbar:)]; self.navigationItem.rightBarButtonItem = barItem; self.toolbar = [[UIToolbar alloc]init]; self.toolbar.frame = CGRectMake(0, 325, self.view.frame.size.width,44); UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]; [self.toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; [self.toolbar setHidden:YES]; [self.view addSubview:self.toolbar]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)showToolbar:(id)sender{ [self.tabBarController.tabBar setHidden:YES]; [self.toolbar setHidden:NO]; } 

当然,这个代码只能在肖像模式下的iPhone上工作。 你将不得不改变风景或iPad的数字…并注意界面方向的变化。

嗨,朋友,这个代码也可以帮助你…

  UIToolbar *toolbar = [[UIToolbar alloc]init]; toolbar.frame = CGRectMake(0, 960, self.view.frame.size.width,44); UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]; // [toolbar setBarStyle:UIBarStyleBlackTranslucent]; [toolbar setAutoresizesSubviews:YES]; [toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; [self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; [self.tabBarController.view addSubview:toolbar]; [self.tabBarController.view bringSubviewToFront:toolbar]; [self.tabBarController.tabBar setHidden:YES]; 

实际上tabbar是视图控制器之一,所以你可以在tabbar本身添加工具栏,解决了以前的问题。有一个伟大的一天!