【发布时间】:2012-01-04 04:25:33
【问题描述】:
我从 IB 中删除了 ToolBar 控件,而是尝试通过代码创建。我尝试了以下在网上找到的代码。我没有在“viewWillAppear”中编写此代码,而是将代码放在同一个 UIViewController 导航栏中的“栏按钮项”中。
- (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 方法(点击栏按钮项)?
【问题讨论】:
-
也许这是我对 IOS 显示的无知,但不是任何视图子类的默认初始化程序
-initWithFrame:?
标签: objective-c ios