1

I am really having a hard time creating a UIToolBar programmatically(no xib file). This is what I would like to create.

But till now, I feel I have not approached it in the right way. I have tried adding a barbuttonitem to it, but I cant create the effect as shown in the png. How should I go about it. Help needed.

Edit: I have added a bigger image

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80
  • 2
    Could you add a larger image? It's hard to see what we're actually looking at and what you're trying to accomplish. To me this looks like a UINavigationBar with a title set. – runmad May 08 '12 at 14:36
  • I tried adding barbuttonitem to the toolbar, but couldn't create the effect as shown in the image. – Xavi Valero May 10 '12 at 15:24
  • @Xavi Valero, I'm confused by your image, I see the nav bar at the bottom, but it looks like you are circling the table view. I'm not sure if that's what you intended but the part that is circled is a table view section in a Table View set to grouped. – matto0 May 14 '12 at 01:58
  • @matto0...ignore the circled part. I just had this image to post. I am only concerned about the bottom bar – Xavi Valero May 14 '12 at 04:31

6 Answers6

3
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"toolbar title" style:UIBarButtonItemStylePlain    target:self     action:@selector(onToolbarTapped:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil];

[self setToolbarItems:toolbarItems animated:NO];
Jean Paul
  • 2,389
  • 5
  • 27
  • 37
2

Create your toolbar with CGRECT to put it where you want

UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)];

You can customize it with background image, title, text color, background color, ...

Next create your button

UIBarButtonItem *theButton = [UIBarButtonItem alloc] initWithTitle:@"button title" style:UIBarButtonItemStylePlain    target:self     action:@selector(selector:)];

Add it to yout toolbar :

NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]

add your toolbar to the current view

[self.view addSubview:mytoolbar]

Just typed not tested, hope it'll help

jbduzan
  • 1,106
  • 1
  • 14
  • 30
1

ToolBar and buttons have custom images

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
    [myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar_40.png"]] autorelease] atIndex:0];

    UIBarButtonItem *barButtonDone = [self createImageButtonItemWithNoTitle:@"button_down.png" target:self action:@selector(closeMe:)];
    UIBarButtonItem *barButtonShare = [self createImageButtonItemWithNoTitle:@"button_share.png" target:self action:@selector(actionShareButton:)];
    UIBarButtonItem *barButtonFlexibleGap = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
    myToolbar.items = [NSArray arrayWithObjects:barButtonDone,barButtonFlexibleGap,barButtonShare,nil];

    [self.view addSubview:myToolbar];
    [myToolbar release];
}

-(void)closeMe:(id)sender
{
    NSLog(@"closeMe");
}

-(void)actionShareButton:(id)sender
{   
    NSLog(@"actionShareButton");
}

-(UIBarButtonItem *)createImageButtonItemWithNoTitle:(NSString *)imagePath target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    UIImage *buttonImage = [[UIImage imageNamed:@"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = 32;
    buttonFrame.size.height = 32;
    [button setFrame:buttonFrame];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    imageView.image = [UIImage imageNamed:imagePath];
    [button addSubview:imageView];
    [imageView release];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];

}

These png files need to be added

  1. toolbar_40.png for toolbar image
  2. button_down.png for button image
  3. button_share.png for button image
  4. button_slice.png for button background
  5. button_slice_over.png for button background (pressed)
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
0
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.backgroundColor = [UIColor clearColor];

Then add a UIBarButtonItem to toolbar.

Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
0
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
myToolbar.barStyle = UIBarStyleBlack;

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
myLabel.text = @"Sperre aufheben";
myLabel.textColor = [UIColor whiteColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];

[myToolbar  addSubview:myLabel];
[self.view addSubview:myToolbar];

[myLabel release];
[myToolbar release];
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
0

Seems like a better fit for a nav bar (at least if you are keeping with the spirit of what you are doing: IE adding a title to the bottom of the screen)

Anyway I created a simple test project and this is what I did to get the functionality pictured, you may have to insert it into the view/controller differently but it gets what you want relatively easy and doesn't use the button bar

- (void)viewDidLoad {

    [super viewDidLoad];

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
    [navBar setBarStyle:UIBarStyleBlack];

    UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Your Title"];
    NSArray *array = [[NSArray alloc] initWithObjects:title, nil];

    [navBar setItems:array];

    [self.view addSubview:navBar];
}
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
matto0
  • 1,065
  • 2
  • 12
  • 26