Search code examples
iphoneiosxcodeipad

How to add UIButton onto a Toolbar Programmatically?


I am struggling to add a UIButton to a toolbar at the bottom of the screen, the problem being it does not appear to go on top of the toolbar, and is hiding underneath it.

The code for toolbar I am trying to add it to (and my Go Back button code below) is;

Please ignore the co ordinates as they are not 100% but the iPhone version at least should show on the toolbar as it around the same area, and I can adjust that to be exact afterwards.

//Insert Main Toolbar On Main View
    if (IS_IPHONE_5) {

            //offsetY = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 6 : 6 * 2;

        offsetY = (1136 - 960) / 2;

        frameRect = CGRectMake(0, SCREEN_HEIGHT - 53, 320 * 2, 53);

    }else if (IS_IPHONE || IS_IPHONE_4) {
            offsetY = 0;
            frameRect = CGRectMake(0, SCREEN_HEIGHT - 53, 320 * 2, 53);

    } else {

        frameRect = CGRectMake(0, 1024 - 53 * 2, 768 * 2, 53 * 2);
    }

  offsetY = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 6 : 6 * 2;

    main_toolbar = [[UIImageView alloc] initWithFrame:frameRect];
    [main_toolbar setImage:[UIImage imageNamed:[g_AppUtils resourceNameForDevice:@"ToolbarBackground" ofType:@"png"]]];
    main_toolbar.userInteractionEnabled = YES;
    [self.view addSubview:main_toolbar];
    [main_toolbar release];

    //Insert Go Back Button On Main Toolbar
    goHome_button = [[UIButton alloc] initWithFrame:frameRect];
    goHome_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [goHome_button setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];

    [goHome_button addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        goHome_button.frame = CGRectMake(55, 410, 46, 42);
    } else {
        goHome_button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    }

   [main_toolbar addSubview:goHome_button];
  [self.view addSubview:goHome_button];

Solution

  • You need to create a UIBarButtonItem using initWithCustomView to associate a UIButton to it. Next adding all your bar button items to the UIToolBar's setItems method. Something like this:

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
    [buttons addObject:barButton];  // buttons is a NSMutableArray
    [mainToolBar setItems:buttons animated:YES];  
    

    For spacing:

    UIBarButtonItem *spacerFlex = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                   target:nil
                                   action:nil];
    
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                   target:nil
                                   action:nil];
    [spacer setWidth:50];  // 50 just an example
    

    Add spcerFlex or spacer into the buttons array.... Per your layout needed.