隐藏UIToolBar以获取UITableViewController的子视图

时间:2009-06-19 00:57:14

标签: iphone

我的主控制器是UITableViewController的子类,底部有一个UIToolBar,当选择一行时,我想显示另一个没有工具栏的视图。如何在子视图中隐藏UIToolBar?现在,它存在于所有子视图中,除非它们被创建为模态。

工具栏在RootController中创建:

self.toolbar = [[UIToolbar alloc] init];
// add tool bar items here
[self.navigationController.view addSubview:toolbar];

RootController显示其子视图:

UIViewController *controller = [[UIViewController alloc] init...]
[self.navigationController pushViewController:controller animated:YES];

RootController在app delegate的applicationDidFinishLaunching中实例化:

RootController *rootcontroller = [[RootController alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootcontroller];

[rootcontroller release];

[window addSubview:[self.navigationController view]];

如果我将工具栏添加到RootController中的[self.view]而不是navigationController的视图中,工具栏将全部消失..

3 个答案:

答案 0 :(得分:2)

您可以尝试隐藏工具栏,然后使用'toolbar.hidden = YES'显示子视图,然后在viewWillAppear方法中,再次使用'toolbar.hidden = NO'显示它。

答案 1 :(得分:0)

另一种选择是使用“removeFromSuperview”

[toolbar removeFromSuperview];

然后在视图中使用viewDidAppear方法,您想重新显示工具栏。 它比viewWillAppear更好用,因为工具栏是在显示视图后添加的。 (对于viewWillAppear,在过渡期间添加了工具栏,因此它有点尴尬。)

答案 2 :(得分:0)

我使用了这个

[toolbar removeFromSuperview];

检查

- (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];



[[self tableView] reloadData];

}

- (void) info_clicked:(id)sender {


[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];

}