ToolBar在以编程方式完成时未显示

时间:2012-01-04 04:25:34

标签: objective-c ios

我从IB中删除了ToolBar控件,但尝试通过代码创建。我尝试了以下在线发现的代码。我没有在“viewWillAppear”中编写此代码,而是将代码放在同一个UIViewController导航栏中的“bar按钮项”中。

- (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方法(单击条形按钮项目)?

请告知。

3 个答案:

答案 0 :(得分:0)

这对我有用....

    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:来接收按钮操作。

- (IBAction)info_clicked:(id)sender{
   NSLog("clicked info_button");
}

答案 1 :(得分:0)

- (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的数字......并注意界面方向的变化。

答案 2 :(得分:0)

嗨朋友这段代码也可以帮到你......

 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中添加工具栏,解决你以前的问题。祝你有个美好的一天!

相关问题