Adding toolbar in a view through coding

Discussion in 'iOS Programming' started by iphonejudy, Feb 19, 2009.

  1. macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    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:mad:" News " style:UIBarButtonItemStyleBordered target:self action:mad: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];


    }
     

Share This Page