如何以编程方式将UIToolbar添加到UITableViewController?
我选择在没有笔尖的情况下使用UITableViewController
。我需要一个UIToolbar,底部有两个按钮。做这件事最简单的方法是什么?如何以编程方式将UIToolbar添加到UITableViewController?
P.S.我知道我可以轻松使用UIViewController
并添加UITableView
,但是我希望整个应用程序看起来一致。
有人可以帮忙吗?
我看到下面的例子,我不知道它的有效性:
(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];
}
最简单的方式做的是建立在UINavigationController
的顶级项目。它已经有一个工具栏,它只是默认隐藏。您可以通过切换toolbarHidden
属性来显示它,只要它在导航控制器层次结构中,表格视图控制器就可以使用它。
在应用程序委托,或者在对象应用程序委托将控制传递到,与UITableViewController
作为根视图控制器创建导航控制器:
- (void)application: (UIApplication *)application
didFinishLaunchingWithOptions: (NSDictionary *)options
{
MyTableViewController *tableViewController;
UINavigationController *navController;
tableViewController = [[ MyTableViewController alloc ]
initWithStyle: UITableViewStylePlain ];
navController = [[ UINavigationController alloc ]
initWithRootViewController: tableViewController ];
[ tableViewController release ];
/* ensure that the toolbar is visible */
navController.toolbarHidden = NO;
self.navigationController = navController;
[ navController release ];
[ self.window addSubview: self.navigationController.view ];
[ self.window makeKeyAndVisible ];
}
然后设置在MyTableViewController
对象中的工具栏项:
- (void)viewDidLoad
{
UIBarButtonItem *buttonItem;
buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back"
style: UIBarButtonItemStyleBordered
target: self
action: @selector(goBack:) ];
self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
[ buttonItem release ];
/* ... additional setup ... */
}
这是一个惊人的建议。我从来不知道UINavigationController有一个默认隐藏的工具栏。我已经在使用它,所以这是一个真正的奖励。也感谢您花时间真正解释一切。 – jini 2011-05-11 15:30:52
您还可以在NavigationController属性检查器中检查“显示工具栏”选项。
但是,然后在所有视图上打开它,最终无论如何在那些不应该使用工具栏的视图上使用代码。 – 2014-05-19 07:49:47
下面是一个简单的例子,这可能有助于
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteMessages)];
UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeMail)];
NSArray *toolbarItems = [NSMutableArray arrayWithObjects:spaceItem, trashItem,spaceItem,composeItem,nil];
self.navigationController.toolbarHidden = NO;
[self setToolbarItems:toolbarItems];
感谢, prodeveloper
需要指出这里最重要的行是[工具栏sizeToFit]没有它 - 显示工具栏,但不接受任何用户交互 – 2016-04-17 11:21:30