PDA

View Full Version : Adding toolbar in a view through coding




iphonejudy
Feb 20, 2009, 12:11 AM
Hi,

I used the blow code for adding toolbar in run time ,But the toolbar is not added.


- (void)viewWillAppear:(BOOL)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 *NewsButton = [[UIBarButtonItem alloc]
initWithTitle:@" News " style:UIBarButtonItemStyleBordered target:self action:@selector(News_clicked:)];




//Create a rectangle for the toolbar
//CGRect rectArea = CGRectMake(100, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

//Reposition and resize the receiver
[toolbar setFrame:rectArea];


[toolbar setItems:[NSArray arrayWithObjects:NewsButton,nil]];
[self.view.window addSubview:toolbar];


//Add the toolbar as a subview to the navigation controller.

[self.view.window addSubview:toolbar];

//Reload the table view
[self.tableView reloadData];


}